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