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