(ImageView) if no padding, then do not set the transform
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / custom-layout-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 "custom-layout-impl.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/actors/actor.h>
22 #include <dali-toolkit/devel-api/controls/control-devel.h>
23
24 namespace Test
25 {
26
27 namespace Internal
28 {
29
30 using Dali::Actor;
31 using Dali::Toolkit::MeasuredSize;
32
33 CustomLayoutPtr CustomLayout::New()
34 {
35   CustomLayoutPtr layout( new CustomLayout() );
36   return layout;
37 }
38
39 void CustomLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
40 {
41   auto accumulatedWidth = 0;
42   auto maxHeight = 0;
43
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
48
49   for( unsigned int i=0; i<GetChildCount(); ++i )
50   {
51     auto childLayout = GetChildAt( i );
52     if( childLayout )
53     {
54       MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
55       accumulatedWidth += childLayout->GetMeasuredWidth();
56       maxHeight = std::max( childLayout->GetMeasuredHeight().mValue, maxHeight );
57     }
58   }
59
60   // Finally, call this method to set the dimensions we would like
61   SetMeasuredDimensions( MeasuredSize( accumulatedWidth ), MeasuredSize( maxHeight ) );
62 }
63
64 void CustomLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
65 {
66   LayoutLength childTop( 0 );
67   LayoutLength childLeft( 0 );
68
69   // We want to vertically align the children to the middle
70   auto height = bottom - top;
71   auto middle = height / 2;
72
73   auto owner = GetOwner();
74   auto actor = Actor::DownCast(owner);
75
76   // Horizontally align the children to the left
77   int count = actor.GetChildCount();
78   int currentLeft = 0;
79
80   for( int i = 0; i < count; i++)
81   {
82     Dali::Toolkit::Control child = Dali::Toolkit::Control::DownCast( actor.GetChildAt( i ) );
83     if( !child )
84     {
85       continue;
86     }
87
88     Dali::Toolkit::LayoutItem childLayout = Dali::Toolkit::DevelControl::GetLayout( child );
89     if( childLayout )
90     {
91       Dali::Toolkit::Internal::LayoutItem& childLayoutImpl = GetImplementation( childLayout );
92
93       auto childWidth = childLayoutImpl.GetMeasuredWidth();
94       auto childHeight = childLayoutImpl.GetMeasuredHeight();
95
96       childTop = middle - (childHeight / 2);
97       childLayoutImpl.Layout( currentLeft, childTop, currentLeft + childWidth, childTop + childHeight );
98       currentLeft += childWidth;
99     }
100   }
101 }
102
103 } // namespace Internal
104
105 } // namespace Test