[5.0] Add animation workaround in LayoutGroup.
[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 Toolkit::LayoutGroup::LayoutId LayoutGroup::Move( LayoutItem& target, LayoutItem& child )
177 {
178   // Remove child from the previous position
179   for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
180   {
181     if( iter->child.Get() == &child )
182     {
183       mImpl->mChildren.erase( iter );
184       break;
185     }
186   }
187
188   // Find target position
189   std::vector< Impl::ChildLayout >::iterator position;
190   for( auto iter = mImpl->mChildren.begin(); iter != mImpl->mChildren.end(); ++iter )
191   {
192     if( iter->child.Get() == &target )
193     {
194       position = iter;
195       break;
196     }
197   }
198
199   Impl::ChildLayout childLayout;
200   childLayout.layoutId = mImpl->mNextLayoutId++;
201   childLayout.child = &child;
202   mImpl->mChildren.insert( position, childLayout );
203
204   RequestLayout();
205
206   return childLayout.layoutId;
207 }
208
209 Toolkit::LayoutGroup::LayoutId LayoutGroup::MoveBack( LayoutItem& child )
210 {
211   // Remove child from the previous position
212   for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
213   {
214     if( iter->child.Get() == &child )
215     {
216       mImpl->mChildren.erase( iter );
217       break;
218     }
219   }
220
221   Impl::ChildLayout childLayout;
222   childLayout.layoutId = mImpl->mNextLayoutId++;
223   childLayout.child = &child;
224   mImpl->mChildren.emplace_back( childLayout );
225
226   RequestLayout();
227
228   return childLayout.layoutId;
229 }
230
231 void LayoutGroup::RemoveAll()
232 {
233   for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; )
234   {
235     RemoveChild( *iter->child.Get() );
236     iter = mImpl->mChildren.erase(iter);
237   }
238 }
239
240 unsigned int LayoutGroup::GetChildCount() const
241 {
242   return mImpl->mChildren.size();
243 }
244
245 LayoutItemPtr LayoutGroup::GetChildAt( unsigned int index ) const
246 {
247   DALI_ASSERT_ALWAYS( index < mImpl->mChildren.size() );
248   return mImpl->mChildren[ index ].child;
249 }
250
251 LayoutItemPtr LayoutGroup::GetChild( Toolkit::LayoutGroup::LayoutId childId ) const
252 {
253   for( auto&& childLayout : mImpl->mChildren )
254   {
255     if( childLayout.layoutId == childId )
256     {
257       return childLayout.child;
258     }
259   }
260   return NULL;
261 }
262
263 Toolkit::LayoutGroup::LayoutId LayoutGroup::GetChildId( LayoutItem& child ) const
264 {
265   for( auto&& childLayout : mImpl->mChildren )
266   {
267     if( childLayout.child.Get() == &child )
268     {
269       return childLayout.layoutId;
270     }
271   }
272   return Toolkit::LayoutGroup::UNKNOWN_ID;
273 }
274
275 void LayoutGroup::OnChildAdd( LayoutItem& child )
276 {
277 }
278
279 void LayoutGroup::OnChildRemove( LayoutItem& child )
280 {
281 }
282
283 void LayoutGroup::DoInitialize()
284 {
285 }
286
287 void LayoutGroup::DoRegisterChildProperties( const std::string& containerType )
288 {
289 }
290
291 void LayoutGroup::OnSetChildProperties( Handle& handle, Property::Index index, Property::Value value )
292 {
293   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties property(" << handle.GetPropertyName(index) << ")\n" );
294
295   if ( ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) &&
296          ( index <= CHILD_PROPERTY_REGISTRATION_MAX_INDEX ) )
297        ||
298        ( index == Toolkit::Control::Property::MARGIN || index == Toolkit::Control::Property::PADDING ) )
299   {
300     // If any child properties are set, must perform relayout
301     for( auto&& child : mImpl->mChildren )
302     {
303       if( child.child->GetOwner() == handle )
304       {
305         child.child->RequestLayout();
306         break;
307       }
308     }
309   }
310 }
311
312 void LayoutGroup::GenerateDefaultChildPropertyValues( Handle child )
313 {
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 );
318 }
319
320 void LayoutGroup::MeasureChildren( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
321 {
322   for( auto&& child : mImpl->mChildren )
323   {
324     //if( (child.mViewFlags & Impl::VISIBILITY_MASK) != Impl::GONE ) // Use owner visibility/enabled/ready
325     {
326       MeasureChild( child.child, widthMeasureSpec, heightMeasureSpec );
327     }
328   }
329 }
330
331 void LayoutGroup::MeasureChild( LayoutItemPtr child,
332                                 MeasureSpec parentWidthMeasureSpec,
333                                 MeasureSpec parentHeightMeasureSpec )
334 {
335   DALI_LOG_TRACE_METHOD( gLogFilter );
336
337   auto childOwner = child->GetOwner();
338
339   auto control = Toolkit::Control::DownCast( childOwner );
340
341 #if defined( DEBUG_ENABLED )
342   if ( control )
343   {
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 );
346   }
347 #endif
348
349
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 child WIDTH_SPEC(%d) child HEIGHT SPEC(%d)\n", desiredWidth, desiredHeight );
354
355   auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
356
357   const MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
358                                                                  padding.start + padding.end,
359                                                                  desiredWidth);
360   const MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
361                                                                   padding.top + padding.bottom,
362                                                                   desiredHeight);
363
364   child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
365 }
366
367 void LayoutGroup::MeasureChildWithMargins( LayoutItemPtr child,
368                                            MeasureSpec parentWidthMeasureSpec, LayoutLength widthUsed,
369                                            MeasureSpec parentHeightMeasureSpec, LayoutLength heightUsed)
370 {
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 );
374
375   auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
376
377   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins child WIDTH_SPEC(%d)\n",  desiredWidth );
378
379   MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
380                                                            LayoutLength( padding.start + padding.end ) +
381                                                            widthUsed, desiredWidth );
382
383   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins child HEIGHT_SPEC(%d)\n",  desiredHeight );
384
385   MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
386                                                             LayoutLength( padding.top + padding.bottom )+
387                                                             heightUsed, desiredHeight );
388
389   child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
390 }
391
392
393 MeasureSpec LayoutGroup::GetChildMeasureSpec(
394   MeasureSpec  measureSpec,
395   LayoutLength padding,
396   LayoutLength childDimension )
397 {
398   auto specMode = measureSpec.GetMode();
399   LayoutLength specSize = measureSpec.GetSize();
400
401   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension(%f)\n", childDimension.AsInteger() );
402
403   LayoutLength size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding
404
405   LayoutLength resultSize = 0;
406   MeasureSpec::Mode resultMode = MeasureSpec::Mode::UNSPECIFIED;
407
408   switch( specMode ) // Parents specMode
409   {
410     // Parent has imposed an exact size on us
411     case MeasureSpec::Mode::EXACTLY:
412     {
413       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::EXACTLY\n");
414       if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
415       {
416         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension MATCH_PARENT\n");
417
418         // Child wants to be our size. So be it.
419         resultSize = size;
420         resultMode = MeasureSpec::Mode::EXACTLY;
421       }
422       else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
423       {
424         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension WRAP_CONTENT\n");
425
426         // Child wants to determine its own size. It can't be
427         // bigger than us.
428         resultSize = size;
429         resultMode = MeasureSpec::Mode::AT_MOST;
430       }
431       else
432       {
433         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension UNSPECIFIED\n");
434         resultSize = childDimension;
435         resultMode = MeasureSpec::Mode::EXACTLY;
436       }
437
438       break;
439     }
440
441       // Parent has imposed a maximum size on us
442     case MeasureSpec::Mode::AT_MOST:
443     {
444       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::AT_MOST\n");
445       if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
446       {
447         // Child wants to be our size, but our size is not fixed.
448         // Constrain child to not be bigger than us.
449         resultSize = size;
450         resultMode = MeasureSpec::Mode::AT_MOST;
451       }
452       else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
453       {
454         // Child wants to determine its own size. It can't be
455         // bigger than us.
456         resultSize = size;
457         resultMode = MeasureSpec::Mode::AT_MOST;
458       }
459       else
460       {
461         // Child wants a specific size... so be it
462         resultSize = childDimension + padding;
463         resultMode = MeasureSpec::Mode::EXACTLY;
464       }
465
466       break;
467     }
468
469       // Parent asked to see how big we want to be
470     case MeasureSpec::Mode::UNSPECIFIED:
471     {
472       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::UNSPECIFIED\n");
473
474       if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
475       {
476         // Child wants to be our size... find out how big it should be
477         resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
478         resultMode = MeasureSpec::Mode::UNSPECIFIED;
479       }
480       else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
481       {
482         // Child wants to determine its own size.... find out how big
483         // it should be
484         resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
485         resultMode = MeasureSpec::Mode::UNSPECIFIED;
486       }
487       else
488       {
489         // Child wants a specific size... let him have it
490         resultSize = childDimension + padding;
491         resultMode = MeasureSpec::Mode::EXACTLY;
492       }
493       break;
494     }
495   }
496
497   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(" << resultSize << ")\n" );
498
499   //noinspection ResourceType
500   return MeasureSpec( resultSize, resultMode );
501 }
502
503
504 void LayoutGroup::OnInitialize()
505 {
506   auto control = Toolkit::Control::DownCast( GetOwner() );
507
508   if( control )
509   {
510     // Take ownership of existing children
511     for( unsigned int childIndex = 0 ; childIndex < control.GetChildCount(); ++childIndex )
512     {
513       ChildAddedToOwnerImpl( control.GetChildAt( childIndex ) );
514     }
515
516     DevelActor::ChildAddedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
517     DevelActor::ChildRemovedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
518     DevelHandle::PropertySetSignal( control ).Connect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
519
520     if( control.GetParent() )
521     {
522       auto parent = Toolkit::Control::DownCast( control.GetParent() );
523       if( parent )
524       {
525         auto parentLayout = Toolkit::LayoutGroup::DownCast( DevelControl::GetLayout( parent ) );
526         if( parentLayout )
527         {
528           Internal::LayoutGroup& parentLayoutImpl = GetImplementation( parentLayout );
529
530           unsigned int count = parent.GetChildCount();
531           unsigned int index = static_cast< unsigned int >( control.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
532
533           // Find insertion position
534           while( ++index < count )
535           {
536             auto sibling = Toolkit::Control::DownCast( parent.GetChildAt( index ) );
537             if( sibling )
538             {
539               auto siblingLayout = DevelControl::GetLayout( sibling );
540               if( siblingLayout )
541               {
542                 Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
543                 parentLayoutImpl.Insert( siblingLayoutImpl, *this );
544                 break;
545               }
546             }
547           }
548
549           if( index >= count )
550           {
551             parentLayoutImpl.Add( *this );
552           }
553         }
554       }
555     }
556
557     RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_OWNER_SET );
558   }
559 }
560
561 void LayoutGroup::OnRegisterChildProperties( const std::string& containerType )
562 {
563   DoRegisterChildProperties( containerType );
564 }
565
566 void LayoutGroup::OnUnparent()
567 {
568   // Remove children
569   RemoveAll();
570
571   auto control = Toolkit::Control::DownCast( GetOwner() );
572   if( control )
573   {
574     DevelActor::ChildAddedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
575     DevelActor::ChildRemovedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
576     DevelHandle::PropertySetSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
577   }
578 }
579
580 void LayoutGroup::RemoveChild( LayoutItem& item )
581 {
582   item.SetParent( nullptr );
583   OnChildRemove( item );
584 }
585
586 void LayoutGroup::ChildAddedToOwner( Actor child )
587 {
588   ChildAddedToOwnerImpl( child );
589   RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_ADD, child, Actor() );
590 }
591
592 void LayoutGroup::ChildAddedToOwnerImpl( Actor child )
593 {
594   LayoutItemPtr childLayout;
595   Toolkit::Control control = Toolkit::Control::DownCast( child );
596
597 #if defined(DEBUG_ENABLED)
598   auto parent = Toolkit::Control::DownCast( GetOwner() );
599   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control(%s) owner control(%s)\n",
600                                               control?control.GetName().c_str():"Invalid",
601                                               parent?parent.GetName().c_str():"Invalid" );
602 #endif
603
604   if( control ) // Can only support adding Controls, not Actors to layout
605   {
606     Internal::Control& childControlImpl = GetImplementation( control );
607     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
608     childLayout = childControlDataImpl.GetLayout();
609
610     if( ! childLayout )
611     {
612       // If the child doesn't already have a layout, then create a LayoutItem or LayoutGroup for it.
613       // If control behaviour flag set to Layout then set a LayoutGroup.
614       if( DevelControl::IsLayoutingRequired( control ) )
615       {
616         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutGroup for control:%s\n",
617                                                     control?control.GetName().c_str():"Invalid" );
618         childLayout = LayoutGroup::New( control );
619       }
620       else
621       {
622         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutItem for control:%s\n",
623                                                     control?control.GetName().c_str():"Invalid" );
624         childLayout = LayoutItem::New( control );
625         childLayout->SetAnimateLayout( IsLayoutAnimated() ); // forces animation inheritance.
626       }
627
628       DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control:" <<  control.GetName() <<
629                        " desiredWidth: " <<  control.GetNaturalSize().width <<
630                        " desiredHeight:"  << control.GetNaturalSize().height );
631
632       childControlDataImpl.SetLayout( *childLayout.Get() );
633
634       Vector3 size = child.GetTargetSize();
635       // If the size of the control is set explicitly make sure that the control size
636       // stays the same after the layout except it is over written with match parent specs.
637
638       auto childControl = Toolkit::Control::DownCast(childLayout->GetOwner());
639
640       bool setWidthExplictly = ( childControl.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) > 0  );
641       if ( size.x != 0 || setWidthExplictly )
642       {
643         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner SetTargetWidth(%f)\n", size.x );
644         childLayout->SetTargetWidth( size.x );
645       }
646
647       bool setHeightExplcitly = ( childControl.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION ) > 0 );
648       if ( size.y != 0 || setHeightExplcitly )
649       {
650         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner SetTargetHeight(%f)\n", size.y);
651         childLayout->SetTargetHeight( size.y );
652       }
653       // Default layout data will be generated by Add().
654     }
655     else
656     {
657       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child(%s) already has a Layout\n", control.GetName().c_str() );
658       LayoutGroupPtr layoutGroup( dynamic_cast< LayoutGroup* >( childLayout.Get() ) );
659       if( !layoutGroup )
660       {
661         // Set only in case of leaf children
662         childLayout->SetAnimateLayout( IsLayoutAnimated() );
663       }
664     }
665
666     Add( *childLayout.Get() );
667   }
668 }
669
670 void LayoutGroup::ChildRemovedFromOwner( Actor child )
671 {
672   Toolkit::Control control = Toolkit::Control::DownCast( child );
673   if( control )
674   {
675     Internal::Control& childControlImpl = GetImplementation( control );
676     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
677     auto childLayout = childControlDataImpl.GetLayout();
678     if( childLayout )
679     {
680       Remove( *childLayout.Get() );
681       RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_REMOVE, child, Actor() );
682     }
683   }
684 }
685
686 void LayoutGroup::ChildOrderChanged( Actor child )
687 {
688   Toolkit::Control childControl = Toolkit::Control::DownCast( child );
689   if( childControl )
690   {
691     Internal::Control& childControlImpl = GetImplementation( childControl );
692     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
693
694     auto childLayout = childControlDataImpl.GetLayout();
695     if( childLayout )
696     {
697       Toolkit::Control control = Toolkit::Control::DownCast( GetOwner() );
698       unsigned int count = control.GetChildCount();
699       unsigned int index = static_cast< unsigned int >( childControl.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
700
701       // Find insertion position
702       while( ++index < count )
703       {
704         auto sibling = Toolkit::Control::DownCast( control.GetChildAt( index ) );
705         if( sibling )
706         {
707           auto siblingLayout = DevelControl::GetLayout( sibling );
708           if( siblingLayout )
709           {
710             Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
711             Move( siblingLayoutImpl, *childLayout );
712             return;
713           }
714         }
715       }
716
717       MoveBack( *childLayout );
718     }
719   }
720 }
721
722 void LayoutGroup::OnOwnerPropertySet( Handle& handle, Property::Index index, Property::Value value )
723 {
724   DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutGroup::OnOwnerPropertySet\n");
725   auto actor = Actor::DownCast( handle );
726   if( actor &&
727       (
728         index == Actor::Property::LAYOUT_DIRECTION  ||
729         index == Toolkit::Control::Property::PADDING  ||
730         index == Toolkit::Control::Property::MARGIN
731       )
732     )
733   {
734     RequestLayout();
735   }
736 }
737
738 void LayoutGroup::OnAnimationStateChanged( bool animateLayout )
739 {
740   // Change children's animation state
741   for( auto&& child : mImpl->mChildren )
742   {
743     LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( child.child.Get() ) );
744     if( ! parentGroup )
745     {
746       // Change state only in case of leaf children
747       child.child->SetAnimateLayout( animateLayout );
748     }
749   }
750 }
751
752 void LayoutGroup::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
753 {
754   auto childCount = GetChildCount();
755
756   DALI_LOG_STREAM( gLogFilter, Debug::Verbose,
757                   "LayoutGroup::OnMeasure Actor Id:" <<  Actor::DownCast(GetOwner()).GetId() <<
758                   " Owner:" <<  Actor::DownCast(GetOwner()).GetName() <<
759                   " Child Count:" << childCount <<
760                   " MeasureSpecs( width:"<<widthMeasureSpec<<", height:"<<heightMeasureSpec );
761
762   auto widthMode = widthMeasureSpec.GetMode();
763   auto heightMode = heightMeasureSpec.GetMode();
764   LayoutLength widthSpecSize = widthMeasureSpec.GetSize();
765   LayoutLength heightSpecSize = heightMeasureSpec.GetSize();
766
767   bool exactWidth ( false );
768   bool exactHeight ( false );
769
770   // Layouting behaviour
771   // EXACT, width and height as provided.
772   // MATCH_PARENT, width and height that of parent
773   // WRAP_CONTENT, take width of widest child and height size of longest child (within given limit)
774   // UNSPECIFIED, take width of widest child and height size of longest child.
775
776   LayoutLength layoutWidth( 0 );
777   LayoutLength layoutHeight( 0 );
778
779   // If LayoutGroup has children then measure children to get max dimensions
780   if ( childCount > 0 )
781   {
782     for( unsigned int i=0; i<childCount; ++i )
783     {
784       auto childLayout = GetChildAt( i );
785       if( childLayout )
786       {
787         auto childControl = Toolkit::Control::DownCast(childLayout->GetOwner());
788
789         // If child control has children check if a ResizePolicy is set on it.  A LayoutItem could be a legacy container.
790         // A legacy container would need it's ResizePolicy to be applied as a MeasureSpec.
791
792         // Check below will be true for legacy containers and controls with layout required set.
793         // Other layouts will have their own OnMeasure (a checked requirement) hence not execute LayoutGroup::OnMeasure.
794         // Controls which have set layout required will not be legacy controls hence should not have a ResizePolicy set.
795         // Only need to map the resize policy the first time as the Layouting system will then set it to FIXED.
796         if( childControl.GetChildCount() > 0 && ! mImpl->mResizePolicyMapped )
797         {
798           // First pass, Static mappings that are not dependant on parent
799           SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::WIDTH );
800           SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::HEIGHT );
801           mImpl->mResizePolicyMapped = true;
802         }
803
804         // Second pass, if any mappings were not possible due to parent size dependancies then calculate an exact desired size for child
805         if( true == childLayout->IsResizePolicyRequired() ) // No need to test child count as this flag would only be set if control had children.
806         {
807           // Get last stored width and height specifications for the child
808           LayoutLength desiredWidth = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
809           LayoutLength desiredHeight = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
810
811           DALI_LOG_INFO( gLogFilter, Debug::General, "LayoutGroup::MeasureChild Initial desired size pre ResizePolicy(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
812
813           childLayout->SetResizePolicyRequired( false ); // clear flag incase in case of changes before next Measure
814           SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( childControl, widthMeasureSpec, heightMeasureSpec, desiredWidth, desiredHeight );
815
816           // Parent dependant ResizePolicies become exact sizes so are now set on the child before it's measured.
817           childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, desiredWidth.AsInteger() );
818           childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, desiredHeight.AsInteger()  );
819
820           DALI_LOG_INFO( gLogFilter, Debug::General, " LayoutGroup::OnMeasure ResizePolicy Required resulting size(%f,%f)\n",  desiredWidth.AsInteger(), desiredHeight.AsInteger() );
821         }
822
823         // Get size of child
824         MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
825         LayoutLength childWidth = childLayout->GetMeasuredWidth();
826         LayoutLength childHeight = childLayout->GetMeasuredHeight();
827
828         Extents childMargin = childLayout->GetMargin();
829         DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure child " << childControl.GetName().c_str() << " width[" << childWidth << "] height[" << childHeight << "]\n" );
830
831         layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end );
832         layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom );
833         DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure calculated child width[" << layoutWidth << "] height[" << layoutHeight << "]\n" );
834       }
835       else
836       {
837         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Not a layout\n" );
838       }
839     }
840
841     Extents padding = GetPadding();
842     layoutWidth += padding.start + padding.end;
843     layoutHeight += padding.top + padding.bottom;
844   }
845   else
846   {
847     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Getting default size as a leaf\n" );
848     // LayoutGroup does not contain any children so must be a leaf
849     layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec );
850     layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec );
851   }
852
853   // Can't exceed specified width
854   if( widthMode == MeasureSpec::Mode::EXACTLY )
855   {
856     exactWidth = true;
857   }
858   else if ( widthMode == MeasureSpec::Mode::AT_MOST )
859   {
860     layoutWidth = std::min( layoutWidth, widthSpecSize );
861   }
862
863   // Can't exceed specified height
864   if( heightMode == MeasureSpec::Mode::EXACTLY )
865   {
866     exactHeight = true;
867   }
868   else if ( heightMode == MeasureSpec::Mode::AT_MOST )
869   {
870     layoutHeight = std::min( layoutHeight, heightSpecSize );
871   }
872
873   layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() );
874   layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() );
875
876   if( exactWidth )
877   {
878     layoutWidth = widthSpecSize;
879   }
880
881   if( exactHeight )
882   {
883     layoutHeight = heightSpecSize;
884   }
885
886   DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnMeasure Measured size(" << layoutWidth << "," << layoutHeight << ") for : " << Actor::DownCast(GetOwner()).GetName() << " \n" );
887   SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) );
888 }
889
890 void LayoutGroup::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
891 {
892   auto count = GetChildCount();
893
894   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" )  << " childCount:" << count );
895
896   for( unsigned int childIndex = 0; childIndex < count; childIndex++)
897   {
898     LayoutItemPtr childLayout = GetChildAt( childIndex );
899     if( childLayout != nullptr )
900     {
901
902       auto childOwner = childLayout->GetOwner();
903       if( !childLayout->IsLayoutAnimated() )
904       {
905         Actor actor = Actor::DownCast( childOwner );
906         if ( actor && DevelActor::IsPositionOrSizeCurrentlyAnimating( actor ) )
907         {
908           continue;
909         }
910       }
911
912       LayoutLength childWidth = childLayout->GetMeasuredWidth();
913       LayoutLength childHeight = childLayout->GetMeasuredHeight();
914       Extents childMargin = childLayout->GetMargin();
915       auto control = Toolkit::Control::DownCast( childOwner );
916       Extents padding = GetPadding();
917
918       auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION );
919       auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT );
920
921       DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnLayout child[" << control.GetName() <<
922                        "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
923
924       // Margin and Padding only supported when child anchor point is TOP_LEFT.
925       int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0;
926       int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0;
927       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY );
928
929       LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX;
930       LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY;
931
932       childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
933     }
934   }
935 }
936
937
938 } // namespace Internal
939 } // namespace Toolkit
940 } // namespace Dali