API for Control to set LayoutRequired flag
[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_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties property(%s)\n", handle.GetPropertyName(index).c_str());
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                                                            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                                                             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   auto size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding
399
400   MeasureSpec::IntType 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_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(%u)\n", resultSize );
493
494
495   //noinspection ResourceType
496   return MeasureSpec( resultSize, resultMode );
497 }
498
499
500 void LayoutGroup::OnInitialize()
501 {
502   auto control = Toolkit::Control::DownCast( GetOwner() );
503
504   if( control )
505   {
506     // Take ownership of existing children
507     for( unsigned int childIndex = 0 ; childIndex < control.GetChildCount(); ++childIndex )
508     {
509       ChildAddedToOwner( control.GetChildAt( childIndex ) );
510     }
511
512     DevelActor::ChildAddedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
513     DevelActor::ChildRemovedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
514     DevelActor::ChildOrderChangedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildOrderChanged );
515     DevelHandle::PropertySetSignal( control ).Connect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
516
517     if( control.GetParent() )
518     {
519       auto parent = Toolkit::Control::DownCast( control.GetParent() );
520       if( parent )
521       {
522         auto parentLayout = Toolkit::LayoutGroup::DownCast( DevelControl::GetLayout( parent ) );
523         if( parentLayout )
524         {
525           Internal::LayoutGroup& parentLayoutImpl = GetImplementation( parentLayout );
526
527           unsigned int count = parent.GetChildCount();
528           unsigned int index = static_cast< unsigned int >( control.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
529
530           // Find insertion position
531           while( ++index < count )
532           {
533             auto sibling = Toolkit::Control::DownCast( parent.GetChildAt( index ) );
534             if( sibling )
535             {
536               auto siblingLayout = DevelControl::GetLayout( sibling );
537               if( siblingLayout )
538               {
539                 Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
540                 parentLayoutImpl.Insert( siblingLayoutImpl, *this );
541                 break;
542               }
543             }
544           }
545
546           if( index >= count )
547           {
548             parentLayoutImpl.Add( *this );
549           }
550         }
551       }
552     }
553   }
554 }
555
556 void LayoutGroup::OnRegisterChildProperties( const std::string& containerType )
557 {
558   DoRegisterChildProperties( containerType );
559 }
560
561 void LayoutGroup::OnUnparent()
562 {
563   // Remove children
564   RemoveAll();
565 }
566
567 void LayoutGroup::RemoveChild( LayoutItem& item )
568 {
569   item.SetParent( nullptr );
570   OnChildRemove( item );
571 }
572
573 void LayoutGroup::ChildAddedToOwner( Actor child )
574 {
575   LayoutItemPtr childLayout;
576   Toolkit::Control control = Toolkit::Control::DownCast( child );
577
578 #if defined(DEBUG_ENABLED)
579   auto parent = Toolkit::Control::DownCast( GetOwner() );
580   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" );
581 #endif
582
583   if( control ) // Can only support adding Controls, not Actors to layout
584   {
585     Internal::Control& childControlImpl = GetImplementation( control );
586     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
587     childLayout = childControlDataImpl.GetLayout();
588
589     if( ! childLayout )
590     {
591       // If the child doesn't already have a layout, then create a LayoutItem or LayoutGroup for it.
592       // If control behaviour flag set to Layout then set a LayoutGroup.
593       if( DevelControl::IsLayoutingRequired( control ) )
594       {
595         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutGroup for control:%s\n", control?control.GetName().c_str():"Invalid" );
596         childLayout = LayoutGroup::New( control );
597       }
598       else
599       {
600         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutItem for control:%s\n", control?control.GetName().c_str():"Invalid" );
601         childLayout = LayoutItem::New( control );
602       }
603       childLayout->SetAnimateLayout( IsLayoutAnimated() ); // @todo this essentially forces animation inheritance. Bad?
604
605       DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner control:" <<  control.GetName().c_str() <<
606                        " desiredWidth: " <<  control.GetNaturalSize().width <<
607                        " desiredHeight:"  << control.GetNaturalSize().height
608                        );
609
610       childControlDataImpl.SetLayout( *childLayout.Get() );
611
612       Vector3 size = child.GetTargetSize();
613       // If the size of the control is set explicitly make sure that the control size
614       // stays the same after the layout except it is over written with match parent specs.
615       if ( size.x != 0 )
616       {
617         childLayout->SetMinimumWidth( size.x );
618       }
619
620       if ( size.y != 0 )
621       {
622         childLayout->SetMinimumHeight( size.y );
623       }
624       // Default layout data will be generated by Add().
625     }
626     else
627     {
628       LayoutGroupPtr layoutGroup( dynamic_cast< LayoutGroup* >( childLayout.Get() ) );
629       if( !layoutGroup )
630       {
631         // Set only in case of leaf children
632         childLayout->SetAnimateLayout( IsLayoutAnimated() );
633       }
634     }
635
636     Add( *childLayout.Get() );
637   }
638 }
639
640 void LayoutGroup::ChildRemovedFromOwner( Actor child )
641 {
642   Toolkit::Control control = Toolkit::Control::DownCast( child );
643   if( control )
644   {
645     Internal::Control& childControlImpl = GetImplementation( control );
646     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
647     auto childLayout = childControlDataImpl.GetLayout();
648     if( childLayout )
649     {
650       Remove( *childLayout.Get() );
651     }
652   }
653 }
654
655 void LayoutGroup::ChildOrderChanged( Actor child )
656 {
657   Toolkit::Control childControl = Toolkit::Control::DownCast( child );
658   if( childControl )
659   {
660     Internal::Control& childControlImpl = GetImplementation( childControl );
661     Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
662
663     auto childLayout = childControlDataImpl.GetLayout();
664     if( childLayout )
665     {
666       Toolkit::Control control = Toolkit::Control::DownCast( GetOwner() );
667       unsigned int count = control.GetChildCount();
668       unsigned int index = static_cast< unsigned int >( childControl.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
669
670       // Find insertion position
671       while( ++index < count )
672       {
673         auto sibling = Toolkit::Control::DownCast( control.GetChildAt( index ) );
674         if( sibling )
675         {
676           auto siblingLayout = DevelControl::GetLayout( sibling );
677           if( siblingLayout )
678           {
679             Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
680             Move( siblingLayoutImpl, *childLayout );
681             return;
682           }
683         }
684       }
685
686       MoveBack( *childLayout );
687     }
688   }
689 }
690
691 void LayoutGroup::OnOwnerPropertySet( Handle& handle, Property::Index index, Property::Value value )
692 {
693   DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutGroup::OnOwnerPropertySet\n");
694   auto actor = Actor::DownCast( handle );
695   if( actor &&
696       (
697         index == Actor::Property::LAYOUT_DIRECTION  ||
698         index == Toolkit::Control::Property::PADDING  ||
699         index == Toolkit::Control::Property::MARGIN
700       )
701     )
702   {
703     RequestLayout();
704   }
705 }
706
707 void LayoutGroup::OnAnimationStateChanged( bool animateLayout )
708 {
709   // Change children's animation state
710   for( auto&& child : mImpl->mChildren )
711   {
712     LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( child.child.Get() ) );
713     if( ! parentGroup )
714     {
715       // Change state only in case of leaf children
716       child.child->SetAnimateLayout( animateLayout );
717     }
718   }
719 }
720
721 void LayoutGroup::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
722 {
723   auto childCount = GetChildCount();
724
725   DALI_LOG_STREAM( gLogFilter, Debug::Verbose,
726                   "LayoutGroup::OnMeasure Actor Id:" <<  Actor::DownCast(GetOwner()).GetId() <<
727                   " Owner:" <<  Actor::DownCast(GetOwner()).GetName() <<
728                   " Child Count:" << childCount <<
729                   " MeasureSpecs( width:"<<widthMeasureSpec<<", height:"<<heightMeasureSpec );
730
731   auto widthMode = widthMeasureSpec.GetMode();
732   auto heightMode = heightMeasureSpec.GetMode();
733   LayoutLength widthSpecSize = widthMeasureSpec.GetSize();
734   LayoutLength heightSpecSize = heightMeasureSpec.GetSize();
735
736   bool exactWidth ( false );
737   bool exactHeight ( false );
738
739   // Default Layouting behaviour if not overridden
740   // EXACT, width and height as provided.
741   // MATCH_PARENT, width and hewidthSpecSizeight that of parent
742
743   // WRAP_CONTENT, take width of widest child and height size of longest child (within given limit)
744   // UNSPECIFIED, take width of widest child and height size of longest child.
745
746   LayoutLength layoutWidth( 0 );
747   LayoutLength layoutHeight( 0 );
748
749   // If LayoutGroup has children then measure children to get max dimensions
750   if ( childCount > 0 )
751   {
752     for( unsigned int i=0; i<childCount; ++i )
753     {
754       auto childLayout = GetChildAt( i );
755       if( childLayout )
756       {
757         auto childOwner = childLayout->GetOwner();
758
759         // Get size of child
760         MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
761         auto childWidth = childLayout->GetMeasuredWidth();
762         auto childHeight = childLayout->GetMeasuredHeight();
763         auto childMargin = childLayout->GetMargin();
764         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure child width[%d] height(%d)\n", childWidth.mValue, childHeight.mValue );
765
766         layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end );
767         layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom );
768         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure calculated child width[%d] calculated height(%d)\n", layoutWidth.mValue, layoutHeight.mValue );
769       }
770       else
771       {
772           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Not a layout\n" );
773       }
774     }
775
776     Extents padding = GetPadding();
777     layoutWidth += padding.start + padding.end;
778     layoutHeight += padding.top + padding.bottom;
779   }
780   else
781   {
782     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Getting default size as a leaf\n" );
783     // LayoutGroup does not contain any children so must be a leaf
784     layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec );
785     layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec );
786   }
787
788   // Can't exceed specified width
789   if( widthMode == MeasureSpec::Mode::EXACTLY )
790   {
791     exactWidth = true;
792   }
793   else if ( widthMode == MeasureSpec::Mode::AT_MOST )
794   {
795     layoutWidth = std::min( layoutWidth, widthSpecSize );
796   }
797
798   // Can't exceed specified height
799   if( heightMode == MeasureSpec::Mode::EXACTLY )
800   {
801     exactHeight = true;
802   }
803   else if ( heightMode == MeasureSpec::Mode::AT_MOST )
804   {
805     layoutHeight = std::min( layoutHeight, heightSpecSize );
806   }
807
808   layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() );
809   layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() );
810
811   if( exactWidth )
812   {
813     layoutWidth = widthSpecSize;
814   }
815
816   if( exactHeight )
817   {
818     layoutHeight = heightSpecSize;
819   }
820
821   DALI_LOG_INFO( gLogFilter, Debug::General, "LayoutGroup::OnMeasure Measured size(%d,%d) for : %s \n", layoutWidth.mValue, layoutHeight.mValue, Actor::DownCast(GetOwner()).GetName().c_str() );
822   SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) );
823 }
824
825 void LayoutGroup::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
826 {
827   auto count = GetChildCount();
828
829   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" )  << " childCount:" << count );
830
831   for( unsigned int childIndex = 0; childIndex < count; childIndex++)
832   {
833     LayoutItemPtr childLayout = GetChildAt( childIndex );
834     if( childLayout != nullptr )
835     {
836
837       auto childOwner = childLayout->GetOwner();
838       auto childWidth = childLayout->GetMeasuredWidth();
839       auto childHeight = childLayout->GetMeasuredHeight();
840       auto childMargin = childLayout->GetMargin();
841       auto control = Toolkit::Control::DownCast( childOwner );
842       Extents padding = GetPadding();
843
844       auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION );
845       auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT );
846
847       DALI_LOG_INFO( gLogFilter, Debug::General, "LayoutGroup::OnLayout child[%s] position(%f,%f) child width[%d] height(%d)\n",
848                       control.GetName().c_str(),
849                       childPosition.x, childPosition.y,
850                       childWidth.mValue, childHeight.mValue
851                       );
852
853       // Margin and Padding only supported when child anchor point is TOP_LEFT.
854       int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0;
855       int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0;
856       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY );
857
858       LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX;
859       LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY;
860
861       childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
862     }
863   }
864 }
865
866
867 } // namespace Internal
868 } // namespace Toolkit
869 } // namespace Dali