[dali_1.4.16] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / layouting / absolute-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/absolute-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
28 namespace
29 {
30 #if defined(DEBUG_ENABLED)
31 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
32 #endif
33 }
34
35 namespace Dali
36 {
37 namespace Toolkit
38 {
39 namespace Internal
40 {
41
42 AbsoluteLayoutPtr AbsoluteLayout::New()
43 {
44   AbsoluteLayoutPtr layout( new AbsoluteLayout() );
45   return layout;
46 }
47
48 AbsoluteLayout::AbsoluteLayout()
49 : LayoutGroup()
50 {
51 }
52
53 AbsoluteLayout::~AbsoluteLayout()
54 {
55 }
56
57 void AbsoluteLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
58 {
59 #if defined(DEBUG_ENABLED)
60   auto actor = Actor::DownCast(GetOwner());
61
62   std::ostringstream oss;
63   oss << "AbsoluteLayout::OnMeasure  ";
64   if( actor )
65   {
66     oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << "  ";
67   }
68   oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
69   DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
70 #endif
71
72   LayoutLength totalHeight( 0 );
73   LayoutLength totalWidth( 0 );
74
75   struct
76   {
77     MeasuredSize::State widthState;
78     MeasuredSize::State heightState;
79   } childState = { MeasuredSize::State::MEASURED_SIZE_OK, MeasuredSize::State::MEASURED_SIZE_OK };
80
81   LayoutLength minPositionX( 0 );
82   LayoutLength minPositionY( 0 );
83   LayoutLength maxPositionX( 0 );
84   LayoutLength maxPositionY( 0 );
85
86   // measure children
87   for( unsigned int i=0; i<GetChildCount(); ++i )
88   {
89     auto childLayout = GetChildAt( i );
90     if( childLayout )
91     {
92       auto childOwner = childLayout->GetOwner();
93
94       // Get size of child
95       MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
96       LayoutLength childWidth = childLayout->GetMeasuredWidth();
97       LayoutLength childHeight = childLayout->GetMeasuredHeight();
98
99       // Determine the width and height needed by the children using their given position and size.
100       // Children could overlap so find the left most and right most child.
101       auto childPosition = childOwner.GetProperty< Vector3 >( Actor::Property::POSITION );
102       LayoutLength childLeft = childPosition.x;
103       LayoutLength childTop = childPosition.y;
104
105       minPositionX = std::min( minPositionX, childLeft );
106       maxPositionX = std::max( maxPositionX, childLeft + childWidth );
107       // Children could overlap so find the highest and lowest child.
108       minPositionY = std::min( minPositionY, childTop );
109       maxPositionY = std::max( maxPositionY, childTop + childHeight );
110
111       // Store current width and height needed to contain all children.
112       totalWidth = maxPositionX - minPositionX;
113       totalHeight = maxPositionY - minPositionY;
114       DALI_LOG_INFO( gLogFilter, Debug::Concise, "AbsoluteLayout::OnMeasure child width(%f) height(%f) \n", totalWidth.AsDecimal(), totalHeight.AsDecimal() );
115
116       if( childLayout->GetMeasuredWidthAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
117       {
118         childState.widthState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
119       }
120       if( childLayout->GetMeasuredHeightAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
121       {
122         childState.heightState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
123       }
124     }
125   }
126
127   MeasuredSize widthSizeAndState = ResolveSizeAndState( totalWidth, widthMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
128   MeasuredSize heightSizeAndState = ResolveSizeAndState( totalHeight, heightMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
129   totalWidth = widthSizeAndState.GetSize();
130   totalHeight = heightSizeAndState.GetSize();
131
132   // Ensure layout respects it's given minimum size
133   totalWidth = std::max( totalWidth, GetSuggestedMinimumWidth() );
134   totalHeight = std::max( totalHeight, GetSuggestedMinimumHeight() );
135
136   widthSizeAndState.SetState( childState.widthState );
137   heightSizeAndState.SetState( childState.heightState );
138
139   SetMeasuredDimensions( ResolveSizeAndState( totalWidth, widthMeasureSpec, childState.widthState ),
140                          ResolveSizeAndState( totalHeight, heightMeasureSpec, childState.heightState ) );
141
142 }
143
144 void AbsoluteLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
145 {
146   // Absolute layout positions it's children at their Actor positions.
147   // Children could overlap or spill outside the parent, as is the nature of absolute positions.
148   auto count = GetChildCount();
149   DALI_LOG_INFO( gLogFilter, Debug::Concise, "AbsoluteLayout::OnLayout child count(%d)\n", count  );
150
151   for( unsigned int i = 0; i < count; i++)
152   {
153     LayoutItemPtr childLayout = GetChildAt( i );
154     if( childLayout != nullptr )
155     {
156       auto childOwner = childLayout->GetOwner();
157       LayoutLength childWidth = childLayout->GetMeasuredWidth();
158       LayoutLength childHeight = childLayout->GetMeasuredHeight();
159
160       auto childPosition = childOwner.GetProperty< Vector3 >( Actor::Property::POSITION );
161
162       LayoutLength childTop = childPosition.y;
163       LayoutLength childLeft = childPosition.x;
164
165       DALI_LOG_STREAM( gLogFilter, Debug::General,
166                        "AbsoluteLayout::OnLayout child[" << Toolkit::Control::DownCast( childOwner ).GetName().c_str() <<
167                        "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
168
169       childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
170     }
171   }
172 }
173
174 } // namespace Internal
175 } // namespace Toolkit
176 } // namespace Dali