e31045f9303d91a30eb71c73900450a539dc3140
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-item-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 #include <dali/integration-api/debug.h>
18
19 #include <dali/public-api/animation/animation.h>
20 #include <dali/public-api/object/type-registry-helper.h>
21 #include <dali-toolkit/public-api/controls/control.h>
22 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
23 #include <dali-toolkit/internal/layouting/layout-item-data-impl.h>
24
25 #if defined(DEBUG_ENABLED)
26     Debug::Filter* gLayoutFilter = Debug::Filter::New( Debug::Verbose, false, "LOG_LAYOUT" );
27 #endif
28
29 namespace
30 {
31 const char* WIDTH_SPECIFICATION_NAME( "widthSpecification" );
32 const char* HEIGHT_SPECIFICATION_NAME( "heightSpecification" );
33
34 const float DEFAULT_TRANSITION_DURATION( 0.5f );
35 }
36
37 namespace Dali
38 {
39 namespace Toolkit
40 {
41 namespace Internal
42 {
43
44 LayoutItem::LayoutItem()
45 : mImpl( new LayoutItem::Impl() ),
46   mSlotDelegate( this )
47 {
48 }
49
50 LayoutItem::~LayoutItem()
51 {
52   // An object with a unique_ptr to an opaque structure must define it's destructor in the translation unit
53   // where the opaque structure is defined. It cannot use the default method in the header file.
54 }
55
56 LayoutItemPtr LayoutItem::New( Handle& owner )
57 {
58   LayoutItemPtr layoutPtr = new LayoutItem();
59   return layoutPtr;
60 }
61
62 void LayoutItem::Initialize( Handle& owner, const std::string& containerType )
63 {
64   mImpl->mOwner = &(owner.GetBaseObject());
65   RegisterChildProperties( containerType );
66   OnInitialize(); // Ensure direct deriving class gets initialized
67   RequestLayout();
68 }
69
70 Handle LayoutItem::GetOwner() const
71 {
72   return Handle::DownCast(BaseHandle(mImpl->mOwner));
73 }
74
75 void LayoutItem::Unparent()
76 {
77   // Enable directly derived types to first remove children
78   OnUnparent();
79
80   // Last, clear owner
81   mImpl->mOwner = NULL;
82 }
83
84 void LayoutItem::SetAnimateLayout( bool animateLayout )
85 {
86   mImpl->mAnimated = animateLayout;
87 }
88
89 bool LayoutItem::IsLayoutAnimated() const
90 {
91   return mImpl->mAnimated;
92 }
93
94 void LayoutItem::RegisterChildProperties( const std::string& containerType )
95 {
96   // Call on derived types
97   auto typeInfo = TypeRegistry::Get().GetTypeInfo( containerType );
98   if( typeInfo )
99   {
100     Property::IndexContainer indices;
101     typeInfo.GetChildPropertyIndices( indices );
102
103     if( std::find( indices.Begin(), indices.End(), Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) ==
104         indices.End() )
105     {
106       ChildPropertyRegistration( typeInfo.GetName(), WIDTH_SPECIFICATION_NAME,
107                                  Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, Property::INTEGER );
108
109       ChildPropertyRegistration( typeInfo.GetName(), HEIGHT_SPECIFICATION_NAME,
110                                  Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, Property::INTEGER );
111     }
112
113     OnRegisterChildProperties( containerType );
114   }
115 }
116
117 void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
118 {
119 }
120
121
122 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
123 {
124   const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
125
126   const bool specChanged =
127     ( widthMeasureSpec  != mImpl->mOldWidthMeasureSpec ) ||
128     ( heightMeasureSpec != mImpl->mOldHeightMeasureSpec );
129
130   const bool isSpecExactly =
131     ( widthMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ) &&
132     ( heightMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY );
133
134   const bool matchesSpecSize =
135     ( GetMeasuredWidth() == widthMeasureSpec.GetSize() ) &&
136     ( GetMeasuredHeight() == heightMeasureSpec.GetSize() );
137
138   const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
139
140   if( forceLayout || needsLayout)
141   {
142     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
143
144     // measure ourselves, this should set the measured dimension flag back
145     OnMeasure( widthMeasureSpec, heightMeasureSpec );
146     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
147
148     // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
149     DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
150                         "Layout's OnMeasure() did not set the measured dimension by calling setMeasuredDimension()" );
151     mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
152   }
153
154   mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
155   mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
156 }
157
158 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
159 {
160   if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
161   {
162     OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
163     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
164   }
165
166   bool changed = SetFrame( l, t, r, b );
167
168   if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
169   {
170     OnLayout( changed, l, t, r, b );
171     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
172   }
173
174   mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
175   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
176 }
177
178 LayoutLength LayoutItem::GetMinimumWidth() const
179 {
180   return mImpl->mMinimumSize.GetWidth();
181 }
182
183 LayoutLength LayoutItem::GetMinimumHeight() const
184 {
185   return mImpl->mMinimumSize.GetHeight();
186 }
187
188 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
189 {
190   mImpl->mMinimumSize.SetWidth( minimumWidth );
191   RequestLayout();
192 }
193
194 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
195 {
196   mImpl->mMinimumSize.SetHeight( minimumHeight );
197   RequestLayout();
198 }
199
200 Extents LayoutItem::GetPadding() const
201 {
202   return mImpl->mPadding;
203 }
204
205 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
206 {
207   LayoutLength result = size;
208   auto specMode = measureSpec.GetMode();
209   auto specSize = measureSpec.GetSize();
210
211   switch (specMode)
212   {
213     case MeasureSpec::Mode::UNSPECIFIED:
214     {
215       result = size;
216       break;
217     }
218     case MeasureSpec::Mode::AT_MOST:
219     case MeasureSpec::Mode::EXACTLY:
220     {
221       result = specSize;
222       break;
223     }
224   }
225   return result;
226 }
227
228 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
229 {
230   SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
231                          GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
232 }
233
234 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
235 {
236 }
237
238 LayoutParent* LayoutItem::GetParent()
239 {
240   return mImpl->mLayoutParent;
241 }
242
243 void LayoutItem::RequestLayout()
244 {
245   // @todo Enforce failure if called in Measure/Layout passes.
246   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
247   Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
248   layoutController.RequestLayout( Toolkit::LayoutItem(this) );
249 }
250
251 bool LayoutItem::IsLayoutRequested() const
252 {
253   return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
254 }
255
256 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
257 {
258   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
259   mImpl->mMeasuredWidth = measuredWidth;
260   mImpl->mMeasuredHeight = measuredHeight;
261 }
262
263 LayoutLength LayoutItem::GetMeasuredWidth() const
264 {
265   // Get the size portion of the measured width
266   return  mImpl->mMeasuredWidth.GetSize();
267 }
268
269 LayoutLength LayoutItem::GetMeasuredHeight() const
270 {
271   return  mImpl->mMeasuredHeight.GetSize();
272 }
273
274 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
275 {
276   return mImpl->mMeasuredWidth;
277 }
278
279 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
280 {
281   return mImpl->mMeasuredHeight;
282 }
283
284 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
285 {
286   auto owner = GetOwner();
287   auto actor = Actor::DownCast(owner);
288   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
289
290   return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
291 }
292
293 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
294 {
295   auto owner = GetOwner();
296   auto actor = Actor::DownCast(owner);
297   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
298
299   return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
300 }
301
302 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
303 {
304   auto specMode = measureSpec.GetMode();
305   LayoutLength specSize = measureSpec.GetSize();
306   MeasuredSize result;
307
308   switch( specMode )
309   {
310     case MeasureSpec::Mode::AT_MOST:
311     {
312       if (specSize < size)
313       {
314         result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
315       }
316       else
317       {
318         result.SetSize( size );
319       }
320       break;
321     }
322
323     case MeasureSpec::Mode::EXACTLY:
324     {
325       result.SetSize( specSize );
326       break;
327     }
328
329     case MeasureSpec::Mode::UNSPECIFIED:
330     default:
331     {
332       result.SetSize( size );
333       break;
334     }
335   }
336
337   result.SetState( childMeasuredState );
338   return result;
339 }
340
341
342 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
343 {
344   bool changed = false;
345
346   DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
347
348   if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
349   {
350     changed = true;
351
352     auto oldWidth = mImpl->mRight - mImpl->mLeft;
353     auto oldHeight = mImpl->mBottom - mImpl->mTop;
354     auto newWidth = right - left;
355     auto newHeight = bottom - top;
356     bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
357
358     mImpl->mLeft = left;
359     mImpl->mTop = top;
360     mImpl->mRight = right;
361     mImpl->mBottom = bottom;
362
363     mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
364
365
366     // Reflect up to parent control
367     auto owner = GetOwner();
368     auto actor = Actor::DownCast(owner);
369     if( actor )
370     {
371       if( mImpl->mAnimated )
372       {
373         auto animation = Animation::New( 0.5f );
374         animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
375                              Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
376         animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
377                              Vector3( right-left, bottom-top, 0.0f ) );
378         animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
379         animation.Play();
380       }
381       else
382       {
383         // @todo Collate into list of Property & Property::Value pairs.
384         actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
385         actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
386       }
387     }
388
389     if( sizeChanged )
390     {
391       SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
392     }
393   }
394   return changed;
395 }
396
397 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
398 {
399   auto owner = GetOwner();
400   auto actor = Actor::DownCast(owner);
401   if( actor )
402   {
403     actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
404   }
405 }
406
407 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
408 {
409   OnSizeChanged( newSize, oldSize );
410 }
411
412
413 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
414 {
415 }
416
417 void LayoutItem::OnInitialize()
418 {
419 }
420
421
422 } // namespace Internal
423 } // namespace Toolkit
424 } // namespace Dali