2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "custom-layout-impl.h"
21 #include <dali/public-api/actors/actor.h>
22 #include <dali-toolkit/devel-api/controls/control-devel.h>
31 using Dali::Toolkit::MeasuredSize;
33 CustomLayoutPtr CustomLayout::New()
35 CustomLayoutPtr layout( new CustomLayout() );
39 void CustomLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
41 auto accumulatedWidth = 0;
44 // In this layout we will:
45 // Measuring the layout with the children in a horizontal configuration, one after another
46 // Set the required width to be the accumulated width of our children
47 // Set the required height to be the maximum height of any of our children
49 for( unsigned int i=0; i<GetChildCount(); ++i )
51 auto childLayout = GetChildAt( i );
54 MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
55 accumulatedWidth += childLayout->GetMeasuredWidth();
56 maxHeight = std::max( childLayout->GetMeasuredHeight().mValue, maxHeight );
60 // Finally, call this method to set the dimensions we would like
61 SetMeasuredDimensions( MeasuredSize( accumulatedWidth ), MeasuredSize( maxHeight ) );
64 void CustomLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
66 LayoutLength childTop( 0 );
67 LayoutLength childLeft( 0 );
69 // We want to vertically align the children to the middle
70 auto height = bottom - top;
71 auto middle = height / 2;
73 auto owner = GetOwner();
74 auto actor = Actor::DownCast(owner);
76 // Horizontally align the children to the left
77 int count = actor.GetChildCount();
80 for( int i = 0; i < count; i++)
82 Dali::Toolkit::Control child = Dali::Toolkit::Control::DownCast( actor.GetChildAt( i ) );
88 Dali::Toolkit::LayoutItem childLayout = Dali::Toolkit::DevelControl::GetLayout( child );
91 Dali::Toolkit::Internal::LayoutItem& childLayoutImpl = GetImplementation( childLayout );
93 auto childWidth = childLayoutImpl.GetMeasuredWidth();
94 auto childHeight = childLayoutImpl.GetMeasuredHeight();
96 childTop = middle - (childHeight / 2);
97 childLayoutImpl.Layout( currentLeft, childTop, currentLeft + childWidth, childTop + childHeight );
98 currentLeft += childWidth;
103 } // namespace Internal