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