[dali_1.4.0] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-group-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/devel-api/layouting/layout-group-impl.h>
19
20 // EXTERNAL INCLUDES
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>
25
26 // INTERNAL INCLUDES
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>
31
32 namespace
33 {
34 #if defined(DEBUG_ENABLED)
35 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
36 #endif
37 }
38
39 namespace Dali
40 {
41 namespace Toolkit
42 {
43 namespace Internal
44 {
45
46 LayoutGroup::LayoutGroup()
47 : mImpl( new LayoutGroup::Impl() ),
48   mSlotDelegate(this)
49 {
50 }
51
52 LayoutGroupPtr LayoutGroup::New( Handle& owner )
53 {
54   LayoutGroupPtr layoutPtr = new LayoutGroup();
55   return layoutPtr;
56 }
57
58 LayoutGroup::~LayoutGroup()
59 {
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.
62   RemoveAll();
63 }
64
65 Toolkit::LayoutGroup::LayoutId LayoutGroup::Add( LayoutItem& child )
66 {
67   LayoutParent* oldParent = child.GetParent();
68   if( oldParent )
69   {
70     LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) );
71     if( parentGroup )
72     {
73       parentGroup->Remove( child );
74     }
75   }
76
77   Impl::ChildLayout childLayout;
78   childLayout.layoutId = mImpl->mNextLayoutId++;
79   childLayout.child = &child;
80   mImpl->mChildren.emplace_back( childLayout );
81
82   child.SetParent( this );
83
84   auto owner = child.GetOwner();
85
86   // If the owner does not have any LayoutItem child properties, add them
87   if( ! DevelHandle::DoesCustomPropertyExist( owner, Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) )
88   {
89     // Set default properties for LayoutGroup and LayoutItem.
90     // Deriving classes can override OnChildAdd() to add their own default properties
91     GenerateDefaultChildPropertyValues( owner );
92   }
93
94   // Inform deriving classes that this child has been added
95   OnChildAdd( *childLayout.child.Get() );
96
97   // Now listen to future changes to the child properties.
98   DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties );
99
100   RequestLayout();
101
102   return childLayout.layoutId;
103 }
104
105 void LayoutGroup::Remove( Toolkit::LayoutGroup::LayoutId childId )
106 {
107   for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
108   {
109     if( iter->layoutId == childId )
110     {
111       RemoveChild( *iter->child.Get() );
112       mImpl->mChildren.erase(iter);
113       break;
114     }
115   }
116   RequestLayout();
117 }
118
119 void LayoutGroup::Remove( LayoutItem& child )
120 {
121   for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
122   {
123     if( iter->child.Get() == &child )
124     {
125       RemoveChild( *iter->child.Get() );
126       mImpl->mChildren.erase(iter);
127       break;
128     }
129   }
130   RequestLayout();
131 }
132
133 Toolkit::LayoutGroup::LayoutId LayoutGroup::Insert( LayoutItem& target, LayoutItem& child )
134 {
135   LayoutParent* oldParent = child.GetParent();
136   if( oldParent )
137   {
138     LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) );
139     if( parentGroup )
140     {
141       parentGroup->Remove( child );
142     }
143   }
144
145   // Find target position
146   std::vector< Impl::ChildLayout >::iterator position;
147   for( auto iter = mImpl->mChildren.begin(); iter != mImpl->mChildren.end(); ++iter )
148   {
149     if( iter->child.Get() == &target )
150     {
151       position = iter;
152       break;
153     }
154   }
155
156   Impl::ChildLayout childLayout;
157   childLayout.layoutId = mImpl->mNextLayoutId++;
158   childLayout.child = &child;
159   mImpl->mChildren.insert( position, childLayout );
160
161   child.SetParent( this );
162
163   auto owner = child.GetOwner();
164
165   // Inform deriving classes that this child has been added
166   OnChildAdd( *childLayout.child.Get() );
167
168   // Now listen to future changes to the child properties.
169   DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties );
170
171   RequestLayout();
172
173   return childLayout.layoutId;
174 }
175
176 void LayoutGroup::RemoveAll()
177 {
178   for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; )
179   {
180     RemoveChild( *iter->child.Get() );
181     iter = mImpl->mChildren.erase(iter);
182   }
183 }
184
185 unsigned int LayoutGroup::GetChildCount() const
186 {
187   return mImpl->mChildren.size();
188 }
189
190 LayoutItemPtr LayoutGroup::GetChildAt( unsigned int index ) const
191 {
192   DALI_ASSERT_ALWAYS( index < mImpl->mChildren.size() );
193   return mImpl->mChildren[ index ].child;
194 }
195
196 LayoutItemPtr LayoutGroup::GetChild( Toolkit::LayoutGroup::LayoutId childId ) const
197 {
198   for( auto&& childLayout : mImpl->mChildren )
199   {
200     if( childLayout.layoutId == childId )
201     {
202       return childLayout.child;
203     }
204   }
205   return NULL;
206 }
207
208 Toolkit::LayoutGroup::LayoutId LayoutGroup::GetChildId( LayoutItem& child ) const
209 {
210   for( auto&& childLayout : mImpl->mChildren )
211   {
212     if( childLayout.child.Get() == &child )
213     {
214       return childLayout.layoutId;
215     }
216   }
217   return Toolkit::LayoutGroup::UNKNOWN_ID;
218 }
219
220 void LayoutGroup::OnChildAdd( LayoutItem& child )
221 {
222 }
223
224 void LayoutGroup::OnChildRemove( LayoutItem& child )
225 {
226 }
227
228 void LayoutGroup::DoInitialize()
229 {
230 }
231
232 void LayoutGroup::DoRegisterChildProperties( const std::string& containerType )
233 {
234 }
235
236 void LayoutGroup::OnSetChildProperties( Handle& handle, Property::Index index, Property::Value value )
237 {
238   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties property(" << handle.GetPropertyName(index) << ")\n" );
239
240   if ( ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) &&
241          ( index <= CHILD_PROPERTY_REGISTRATION_MAX_INDEX ) )
242        ||
243        ( index == Toolkit::Control::Property::MARGIN || index == Toolkit::Control::Property::PADDING ) )
244   {
245     // If any child properties are set, must perform relayout
246     for( auto&& child : mImpl->mChildren )
247     {
248       if( child.child->GetOwner() == handle )
249       {
250         child.child->RequestLayout();
251         break;
252       }
253     }
254   }
255 }
256
257 void LayoutGroup::GenerateDefaultChildPropertyValues( Handle child )
258 {
259   child.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION,
260                      Toolkit::ChildLayoutData::WRAP_CONTENT );
261   child.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,
262                      Toolkit::ChildLayoutData::WRAP_CONTENT );
263 }
264
265 void LayoutGroup::MeasureChildren( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
266 {
267   for( auto&& child : mImpl->mChildren )
268   {
269     //if( (child.mViewFlags & Impl::VISIBILITY_MASK) != Impl::GONE ) // Use owner visibility/enabled/ready
270     {
271       MeasureChild( child.child, widthMeasureSpec, heightMeasureSpec );
272     }
273   }
274 }
275
276 void LayoutGroup::MeasureChild( LayoutItemPtr child,
277                                 MeasureSpec parentWidthMeasureSpec,
278                                 MeasureSpec parentHeightMeasureSpec )
279 {
280   DALI_LOG_TRACE_METHOD( gLogFilter );
281
282   auto childOwner = child->GetOwner();
283
284   auto control = Toolkit::Control::DownCast( childOwner );
285
286 #if defined( DEBUG_ENABLED )
287   if ( control )
288   {
289     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild(%s) natural size(%f, %f)\n",
290                    control.GetName().c_str(), control.GetNaturalSize().width, control.GetNaturalSize().height );
291   }
292 #endif
293
294
295   // Get last stored width and height specifications for the child
296   auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
297   auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
298   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild desiredWidth(%d) desiredHeight(%d)\n", desiredWidth, desiredHeight );
299
300   auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
301
302   const MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
303                                                                  padding.start + padding.end,
304                                                                  desiredWidth);
305   const MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
306                                                                   padding.top + padding.bottom,
307                                                                   desiredHeight);
308
309   child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
310 }
311
312 void LayoutGroup::MeasureChildWithMargins( LayoutItemPtr child,
313                                            MeasureSpec parentWidthMeasureSpec, LayoutLength widthUsed,
314                                            MeasureSpec parentHeightMeasureSpec, LayoutLength heightUsed)
315 {
316   auto childOwner = child->GetOwner();
317   auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
318   auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
319
320   auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
321
322   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredWidth(%d)\n",  desiredWidth );
323
324   MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
325                                                            LayoutLength( padding.start + padding.end ) +
326                                                            widthUsed, desiredWidth );
327
328   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredHeight(%d)\n",  desiredHeight );
329
330   MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
331                                                             LayoutLength( padding.top + padding.bottom )+
332                                                             heightUsed, desiredHeight );
333
334   child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
335 }
336
337
338 MeasureSpec LayoutGroup::GetChildMeasureSpec(
339   MeasureSpec  measureSpec,
340   LayoutLength padding,
341   LayoutLength childDimension )
342 {
343   auto specMode = measureSpec.GetMode();
344   LayoutLength specSize = measureSpec.GetSize();
345
346   LayoutLength size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding
347
348   LayoutLength resultSize = 0;
349   MeasureSpec::Mode resultMode = MeasureSpec::Mode::UNSPECIFIED;
350
351   switch( specMode )
352   {
353     // Parent has imposed an exact size on us
354     case MeasureSpec::Mode::EXACTLY:
355     {
356       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::EXACTLY\n");
357       if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
358       {
359         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension MATCH_PARENT\n");
360
361         // Child wants to be our size. So be it.
362         resultSize = size;
363         resultMode = MeasureSpec::Mode::EXACTLY;
364       }
365       else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
366       {
367         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension WRAP_CONTENT\n");
368
369         // Child wants to determine its own size. It can't be
370         // bigger than us.
371         resultSize = size;
372         resultMode = MeasureSpec::Mode::AT_MOST;
373       }
374       else
375       {
376         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension UNSPECIFIED\n");
377         resultSize = childDimension;
378         resultMode = MeasureSpec::Mode::EXACTLY;
379       }
380
381       break;
382     }
383
384       // Parent has imposed a maximum size on us
385     case MeasureSpec::Mode::AT_MOST:
386     {
387       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::AT_MOST\n");
388       if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
389       {
390         // Child wants to be our size, but our size is not fixed.
391         // Constrain child to not be bigger than us.
392         resultSize = size;
393         resultMode = MeasureSpec::Mode::AT_MOST;
394       }
395       else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
396       {
397         // Child wants to determine its own size. It can't be
398         // bigger than us.
399         resultSize = size;
400         resultMode = MeasureSpec::Mode::AT_MOST;
401       }
402       else
403       {
404         // Child wants a specific size... so be it
405         resultSize = childDimension + padding;
406         resultMode = MeasureSpec::Mode::EXACTLY;
407       }
408
409       break;
410     }
411
412       // Parent asked to see how big we want to be
413     case MeasureSpec::Mode::UNSPECIFIED:
414     {
415       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::UNSPECIFIED\n");
416
417       if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
418       {
419         // Child wants to be our size... find out how big it should be
420         resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
421         resultMode = MeasureSpec::Mode::UNSPECIFIED;
422       }
423       else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
424       {
425         // Child wants to determine its own size.... find out how big
426         // it should be
427         resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
428         resultMode = MeasureSpec::Mode::UNSPECIFIED;
429       }
430       else
431       {
432         // Child wants a specific size... let him have it
433         resultSize = childDimension + padding;
434         resultMode = MeasureSpec::Mode::EXACTLY;
435       }
436       break;
437     }
438   }
439
440   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(" << resultSize << ")\n" );
441
442   //noinspection ResourceType
443   return MeasureSpec( resultSize, resultMode );
444 }
445
446
447 void LayoutGroup::OnInitialize()
448 {
449   auto control = Toolkit::Control::DownCast( GetOwner() );
450
451   if( control )
452   {
453     // Take ownership of existing children
454     for( unsigned int childIndex = 0 ; childIndex < control.GetChildCount(); ++childIndex )
455     {
456       ChildAddedToOwnerImpl( control.GetChildAt( childIndex ) );
457     }
458
459     DevelActor::ChildAddedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
460     DevelActor::ChildRemovedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
461     DevelHandle::PropertySetSignal( control ).Connect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
462
463     if( control.GetParent() )
464     {
465       auto parent = Toolkit::Control::DownCast( control.GetParent() );
466       if( parent )
467       {
468         auto parentLayout = Toolkit::LayoutGroup::DownCast( DevelControl::GetLayout( parent ) );
469         if( parentLayout )
470         {
471           Internal::LayoutGroup& parentLayoutImpl = GetImplementation( parentLayout );
472
473           unsigned int count = parent.GetChildCount();
474           unsigned int index = static_cast< unsigned int >( control.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
475
476           // Find insertion position
477           while( ++index < count )
478           {
479             auto sibling = Toolkit::Control::DownCast( parent.GetChildAt( index ) );
480             if( sibling )
481             {
482               auto siblingLayout = DevelControl::GetLayout( sibling );
483               if( siblingLayout )
484               {
485                 Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
486                 parentLayoutImpl.Insert( siblingLayoutImpl, *this );
487                 break;
488               }
489             }
490           }
491
492           if( index >= count )
493           {
494             parentLayoutImpl.Add( *this );
495           }
496         }
497       }
498     }
499
500     RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_OWNER_SET );
501   }
502 }
503
504 void LayoutGroup::OnRegisterChildProperties( const std::string& containerType )
505 {
506   DoRegisterChildProperties( containerType );
507 }
508
509 void LayoutGroup::OnUnparent()
510 {
511   // Remove children
512   RemoveAll();
513
514   auto control = Toolkit::Control::DownCast( GetOwner() );
515   if( control )
516   {
517     DevelActor::ChildAddedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
518     DevelActor::ChildRemovedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
519     DevelHandle::PropertySetSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
520   }
521 }
522
523 void LayoutGroup::RemoveChild( LayoutItem& item )
524 {
525   item.SetParent( nullptr );
526   OnChildRemove( item );
527 }
528
529 void LayoutGroup::ChildAddedToOwner( Actor child )
530 {
531   ChildAddedToOwnerImpl( child );
532   RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_ADD, child, Actor() );
533 }
534
535 void LayoutGroup::ChildAddedToOwnerImpl( Actor child )
536 {
537   LayoutItemPtr childLayout;
538   Toolkit::Control control = Toolkit::Control::DownCast( child );
539
540 #if defined(DEBUG_ENABLED)
541   auto parent = Toolkit::Control::DownCast( GetOwner() );
542   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control(%s) owner control(%s)\n",
543                                               control?control.GetName().c_str():"Invalid",
544                                               parent?parent.GetName().c_str():"Invalid" );
545 #endif
546
547   if( control ) // Can only support adding Controls, not Actors to layout
548   {
549     Internal::Control& childControlImpl = GetImplementation( control );
550     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
551     childLayout = childControlDataImpl.GetLayout();
552
553     if( ! childLayout )
554     {
555       // If the child doesn't already have a layout, then create a LayoutItem or LayoutGroup for it.
556       // If control behaviour flag set to Layout then set a LayoutGroup.
557       if( DevelControl::IsLayoutingRequired( control ) )
558       {
559         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutGroup for control:%s\n",
560                                                     control?control.GetName().c_str():"Invalid" );
561         childLayout = LayoutGroup::New( control );
562       }
563       else
564       {
565         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutItem for control:%s\n",
566                                                     control?control.GetName().c_str():"Invalid" );
567         childLayout = LayoutItem::New( control );
568         childLayout->SetAnimateLayout( IsLayoutAnimated() ); // forces animation inheritance.
569       }
570
571       DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control:" <<  control.GetName() <<
572                        " desiredWidth: " <<  control.GetNaturalSize().width <<
573                        " desiredHeight:"  << control.GetNaturalSize().height );
574
575       childControlDataImpl.SetLayout( *childLayout.Get() );
576
577       Vector3 size = child.GetTargetSize();
578       // If the size of the control is set explicitly make sure that the control size
579       // stays the same after the layout except it is over written with match parent specs.
580       if ( size.x != 0 )
581       {
582         childLayout->SetMinimumWidth( size.x );
583       }
584
585       if ( size.y != 0 )
586       {
587         childLayout->SetMinimumHeight( size.y );
588       }
589       // Default layout data will be generated by Add().
590     }
591     else
592     {
593       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child(%s) already has a Layout\n", control.GetName().c_str() );
594       LayoutGroupPtr layoutGroup( dynamic_cast< LayoutGroup* >( childLayout.Get() ) );
595       if( !layoutGroup )
596       {
597         // Set only in case of leaf children
598         childLayout->SetAnimateLayout( IsLayoutAnimated() );
599       }
600     }
601
602     Add( *childLayout.Get() );
603   }
604 }
605
606 void LayoutGroup::ChildRemovedFromOwner( Actor child )
607 {
608   Toolkit::Control control = Toolkit::Control::DownCast( child );
609   if( control )
610   {
611     Internal::Control& childControlImpl = GetImplementation( control );
612     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
613     auto childLayout = childControlDataImpl.GetLayout();
614     if( childLayout )
615     {
616       Remove( *childLayout.Get() );
617       RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_REMOVE, child, Actor() );
618     }
619   }
620 }
621
622 void LayoutGroup::OnOwnerPropertySet( Handle& handle, Property::Index index, Property::Value value )
623 {
624   DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutGroup::OnOwnerPropertySet\n");
625   auto actor = Actor::DownCast( handle );
626   if( actor &&
627       (
628         index == Actor::Property::LAYOUT_DIRECTION  ||
629         index == Toolkit::Control::Property::PADDING  ||
630         index == Toolkit::Control::Property::MARGIN
631       )
632     )
633   {
634     RequestLayout();
635   }
636 }
637
638 void LayoutGroup::OnAnimationStateChanged( bool animateLayout )
639 {
640   // Change children's animation state
641   for( auto&& child : mImpl->mChildren )
642   {
643     LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( child.child.Get() ) );
644     if( ! parentGroup )
645     {
646       // Change state only in case of leaf children
647       child.child->SetAnimateLayout( animateLayout );
648     }
649   }
650 }
651
652 void LayoutGroup::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
653 {
654   auto childCount = GetChildCount();
655
656   DALI_LOG_STREAM( gLogFilter, Debug::Verbose,
657                   "LayoutGroup::OnMeasure Actor Id:" <<  Actor::DownCast(GetOwner()).GetId() <<
658                   " Owner:" <<  Actor::DownCast(GetOwner()).GetName() <<
659                   " Child Count:" << childCount <<
660                   " MeasureSpecs( width:"<<widthMeasureSpec<<", height:"<<heightMeasureSpec );
661
662   auto widthMode = widthMeasureSpec.GetMode();
663   auto heightMode = heightMeasureSpec.GetMode();
664   LayoutLength widthSpecSize = widthMeasureSpec.GetSize();
665   LayoutLength heightSpecSize = heightMeasureSpec.GetSize();
666
667   bool exactWidth ( false );
668   bool exactHeight ( false );
669
670   // Layouting behaviour
671   // EXACT, width and height as provided.
672   // MATCH_PARENT, width and height that of parent
673   // WRAP_CONTENT, take width of widest child and height size of longest child (within given limit)
674   // UNSPECIFIED, take width of widest child and height size of longest child.
675
676   LayoutLength layoutWidth( 0 );
677   LayoutLength layoutHeight( 0 );
678
679   // If LayoutGroup has children then measure children to get max dimensions
680   if ( childCount > 0 )
681   {
682     for( unsigned int i=0; i<childCount; ++i )
683     {
684       auto childLayout = GetChildAt( i );
685       if( childLayout )
686       {
687         auto childControl = Toolkit::Control::DownCast(childLayout->GetOwner());
688
689         // If child control has children check if a ResizePolicy is set on it.  A LayoutItem could be a legacy container.
690         // A legacy container would need it's ResizePolicy to be applied as a MeasureSpec.
691
692         // Check below will be true for legacy containers and controls with layout required set.
693         // Other layouts will have their own OnMeasure (a checked requirement) hence not execute LayoutGroup::OnMeasure.
694         // Controls which have set layout required will not be legacy controls hence should not have a ResizePolicy set.
695         // Only need to map the resize policy the first time as the Layouting system will then set it to FIXED.
696         if( childControl.GetChildCount() > 0 && ! mImpl->mResizePolicyMapped )
697         {
698           // First pass, Static mappings that are not dependant on parent
699           SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::WIDTH );
700           SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::HEIGHT );
701           mImpl->mResizePolicyMapped = true;
702         }
703
704         // Second pass, if any mappings were not possible due to parent size dependancies then calculate an exact desired size for child
705         if( true == childLayout->IsResizePolicyRequired() ) // No need to test child count as this flag would only be set if control had children.
706         {
707           // Get last stored width and height specifications for the child
708           LayoutLength desiredWidth = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
709           LayoutLength desiredHeight = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
710
711           DALI_LOG_INFO( gLogFilter, Debug::General, "LayoutGroup::MeasureChild Initial desired size pre ResizePolicy(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
712
713           childLayout->SetResizePolicyRequired( false ); // clear flag incase in case of changes before next Measure
714           SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( childControl, widthMeasureSpec, heightMeasureSpec, desiredWidth, desiredHeight );
715
716           // Parent dependant ResizePolicies become exact sizes so are now set on the child before it's measured.
717           childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, desiredWidth.AsInteger() );
718           childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, desiredHeight.AsInteger()  );
719
720           DALI_LOG_INFO( gLogFilter, Debug::General, " LayoutGroup::OnMeasure ResizePolicy Required resulting size(%f,%f)\n",  desiredWidth.AsInteger(), desiredHeight.AsInteger() );
721         }
722
723         // Get size of child
724         MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
725         LayoutLength childWidth = childLayout->GetMeasuredWidth();
726         LayoutLength childHeight = childLayout->GetMeasuredHeight();
727
728         Extents childMargin = childLayout->GetMargin();
729         DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure child " << childControl.GetName().c_str() << " width[" << childWidth << "] height[" << childHeight << "]\n" );
730
731         layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end );
732         layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom );
733         DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure calculated child width[" << layoutWidth << "] height[" << layoutHeight << "]\n" );
734       }
735       else
736       {
737         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Not a layout\n" );
738       }
739     }
740
741     Extents padding = GetPadding();
742     layoutWidth += padding.start + padding.end;
743     layoutHeight += padding.top + padding.bottom;
744   }
745   else
746   {
747     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Getting default size as a leaf\n" );
748     // LayoutGroup does not contain any children so must be a leaf
749     layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec );
750     layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec );
751   }
752
753   // Can't exceed specified width
754   if( widthMode == MeasureSpec::Mode::EXACTLY )
755   {
756     exactWidth = true;
757   }
758   else if ( widthMode == MeasureSpec::Mode::AT_MOST )
759   {
760     layoutWidth = std::min( layoutWidth, widthSpecSize );
761   }
762
763   // Can't exceed specified height
764   if( heightMode == MeasureSpec::Mode::EXACTLY )
765   {
766     exactHeight = true;
767   }
768   else if ( heightMode == MeasureSpec::Mode::AT_MOST )
769   {
770     layoutHeight = std::min( layoutHeight, heightSpecSize );
771   }
772
773   layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() );
774   layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() );
775
776   if( exactWidth )
777   {
778     layoutWidth = widthSpecSize;
779   }
780
781   if( exactHeight )
782   {
783     layoutHeight = heightSpecSize;
784   }
785
786   DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnMeasure Measured size(" << layoutWidth << "," << layoutHeight << ") for : " << Actor::DownCast(GetOwner()).GetName() << " \n" );
787   SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) );
788 }
789
790 void LayoutGroup::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
791 {
792   auto count = GetChildCount();
793
794   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" )  << " childCount:" << count );
795
796   for( unsigned int childIndex = 0; childIndex < count; childIndex++)
797   {
798     LayoutItemPtr childLayout = GetChildAt( childIndex );
799     if( childLayout != nullptr )
800     {
801
802       auto childOwner = childLayout->GetOwner();
803       LayoutLength childWidth = childLayout->GetMeasuredWidth();
804       LayoutLength childHeight = childLayout->GetMeasuredHeight();
805       Extents childMargin = childLayout->GetMargin();
806       auto control = Toolkit::Control::DownCast( childOwner );
807       Extents padding = GetPadding();
808
809       auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION );
810       auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT );
811
812       DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnLayout child[" << control.GetName() <<
813                        "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
814
815       // Margin and Padding only supported when child anchor point is TOP_LEFT.
816       int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0;
817       int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0;
818       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY );
819
820       LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX;
821       LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY;
822
823       childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
824     }
825   }
826 }
827
828
829 } // namespace Internal
830 } // namespace Toolkit
831 } // namespace Dali