Control impl layout code for Margin removed
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / layouting / bin-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 <dali-toolkit/internal/layouting/bin-layout-impl.h>
19
20 //INTERNAL HEADERS
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/common/extents.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali-toolkit/devel-api/layouting/layout-item.h>
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
27 #include <dali-toolkit/internal/layouting/size-negotiation-mapper.h>
28
29 namespace
30 {
31 #if defined(DEBUG_ENABLED)
32 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
33 #endif
34 }
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42
43 BinLayoutPtr BinLayout::New()
44 {
45   BinLayoutPtr layout( new BinLayout() );
46   return layout;
47 }
48
49 BinLayout::BinLayout()
50 : LayoutGroup()
51 {
52 }
53
54 BinLayout::~BinLayout()
55 {
56 }
57
58 void BinLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
59 {
60 #if defined(DEBUG_ENABLED)
61   auto actor = Actor::DownCast(GetOwner());
62
63   std::ostringstream oss;
64   oss << "BinLayout::OnMeasure  ";
65   if( actor )
66   {
67     oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << "  ";
68   }
69   oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
70   DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
71 #endif
72
73   auto childCount = GetChildCount();
74
75   DALI_LOG_STREAM( gLogFilter, Debug::Verbose,
76                   "BinLayout::OnMeasure Actor Id:" <<  Actor::DownCast(GetOwner()).GetId() <<
77                   " Owner:" <<  Actor::DownCast(GetOwner()).GetName() <<
78                   " Child Count:" << childCount <<
79                   " MeasureSpecs( width:"<<widthMeasureSpec<<", height:"<<heightMeasureSpec );
80
81   auto widthMode = widthMeasureSpec.GetMode();
82   auto heightMode = heightMeasureSpec.GetMode();
83   LayoutLength widthSpecSize = widthMeasureSpec.GetSize();
84   LayoutLength heightSpecSize = heightMeasureSpec.GetSize();
85
86   bool exactWidth ( false );
87   bool exactHeight ( false );
88
89   // Layouting behaviour
90   // EXACT, width and height as provided.
91   // MATCH_PARENT, width and height that of parent
92   // WRAP_CONTENT, take width of widest child and height size of longest child (within given limit)
93   // UNSPECIFIED, take width of widest child and height size of longest child.
94
95   LayoutLength layoutWidth( 0 );
96   LayoutLength layoutHeight( 0 );
97
98   // If BinLayout has children then measure children to get max dimensions
99   if ( childCount > 0 )
100   {
101     for( unsigned int i=0; i<childCount; ++i )
102     {
103       auto childLayout = GetChildAt( i );
104       if( childLayout )
105       {
106         auto childControl = Toolkit::Control::DownCast(childLayout->GetOwner());
107
108         // If child control has children check if a ResizePolicy is set on it.  A LayoutItem could be a legacy container.
109         // A legacy container would need it's ResizePolicy to be applied as a MeasureSpec.
110
111         // Check below will be true for legacy containers and for controls with the layout flag required set.
112         // Other layouts will have their own OnMeasure (a requirement) hence not execute BinLayout::OnMeasure.
113         // Controls which have set the layout required flag will not be legacy controls hence should not have a ResizePolicy set.
114         if( childControl.GetChildCount() > 0 )
115         {
116           // First pass, Static mappings that are not dependant on parent
117           SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::WIDTH );
118           SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::HEIGHT );
119         }
120
121         // Second pass, if any mappings were not possible due to parent size dependencies then calculate an exact desired size for child
122         if( true == childLayout->IsResizePolicyRequired() ) // No need to test child count as this flag would only be set if control had children.
123         {
124           // Get last stored width and height specifications for the child
125           LayoutLength desiredWidth = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
126           LayoutLength desiredHeight = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
127
128           DALI_LOG_INFO( gLogFilter, Debug::General, "BinLayout::MeasureChild Initial desired size pre ResizePolicy(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
129
130           childLayout->SetResizePolicyRequired( false ); // clear flag incase in case of changes before next Measure
131           SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( childControl, widthMeasureSpec, heightMeasureSpec, desiredWidth, desiredHeight );
132
133           // Parent dependant ResizePolicies become exact sizes so are now set on the child before it's measured.
134           childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, desiredWidth.AsInteger() );
135           childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, desiredHeight.AsInteger()  );
136
137           DALI_LOG_INFO( gLogFilter, Debug::General, " BinLayout::OnMeasure ResizePolicy Required resulting size(%f,%f)\n",  desiredWidth.AsInteger(), desiredHeight.AsInteger() );
138         }
139
140         // Get size of child
141         MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
142         LayoutLength childWidth = childLayout->GetMeasuredWidth();
143         LayoutLength childHeight = childLayout->GetMeasuredHeight();
144
145         Extents childMargin = childLayout->GetMargin();
146
147         // BinLayout width is that of it's widest child and it's height of the tallest child.
148         // MeasureSpec::Mode::UNSPECIFIED will use these sizes
149         layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end );
150         layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom );
151         DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "BinLayout::OnMeasure calculated current BinLayout width[" << layoutWidth << "] height[" << layoutHeight << "]\n" );
152       }
153       else
154       {
155         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "BinLayout::OnMeasure Not a layout\n" );
156       }
157     }
158
159     Extents padding = GetPadding();
160     layoutWidth += padding.start + padding.end;
161     layoutHeight += padding.top + padding.bottom;
162   }
163   else
164   {
165     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "BinLayout::OnMeasure Getting default size as a leaf\n" );
166     // BinLayout does not contain any children so must be a leaf
167     layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec );
168     layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec );
169   }
170
171   // Can't exceed specified width
172   if( widthMode == MeasureSpec::Mode::EXACTLY )
173   {
174     exactWidth = true;
175   }
176   else if ( widthMode == MeasureSpec::Mode::AT_MOST )
177   {
178     layoutWidth = std::min( layoutWidth, widthSpecSize );
179   }
180
181   // Can't exceed specified height
182   if( heightMode == MeasureSpec::Mode::EXACTLY )
183   {
184     exactHeight = true;
185   }
186   else if ( heightMode == MeasureSpec::Mode::AT_MOST )
187   {
188     layoutHeight = std::min( layoutHeight, heightSpecSize );
189   }
190
191   layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() );
192   layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() );
193
194   if( exactWidth )
195   {
196     layoutWidth = widthSpecSize;
197   }
198
199   if( exactHeight )
200   {
201     layoutHeight = heightSpecSize;
202   }
203
204   DALI_LOG_STREAM( gLogFilter, Debug::General, "BinLayout::OnMeasure Measured size(" << layoutWidth << "," << layoutHeight << ") for : " << Actor::DownCast(GetOwner()).GetName() << " \n" );
205   SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) );
206
207 }
208
209 void BinLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
210 {
211     auto count = GetChildCount();
212
213   DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "BinLayout OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" )  << " childCount:" << count );
214
215   for( unsigned int childIndex = 0; childIndex < count; childIndex++)
216   {
217     LayoutItemPtr childLayout = GetChildAt( childIndex );
218     if( childLayout != nullptr )
219     {
220
221       auto childOwner = childLayout->GetOwner();
222       LayoutLength childWidth = childLayout->GetMeasuredWidth();
223       LayoutLength childHeight = childLayout->GetMeasuredHeight();
224       Extents childMargin = childLayout->GetMargin();
225       auto control = Toolkit::Control::DownCast( childOwner );
226       Extents padding = GetPadding();
227
228       auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION );
229       auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT );
230
231       DALI_LOG_STREAM( gLogFilter, Debug::General, "BinLayout::OnLayout child[" << control.GetName() <<
232                        "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
233
234       // Margin and Padding only supported when child anchor point is TOP_LEFT.
235       int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0;
236       int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0;
237       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "BinLayout::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY );
238
239       LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX;
240       LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY;
241
242       childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
243     }
244   }
245 }
246
247 } // namespace Internal
248 } // namespace Toolkit
249 } // namespace Dali