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