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