Merge "Add new layouting support for TextLabel and ImageView." into devel/master
[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, "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 }
304
305 LayoutParent* LayoutItem::GetParent()
306 {
307   return mImpl->mLayoutParent;
308 }
309
310 void LayoutItem::RequestLayout()
311 {
312   Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
313   if ( control )
314   {
315     DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
316   }
317   // @todo Enforce failure if called in Measure/Layout passes.
318   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
319   Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
320   layoutController.RequestLayout( Toolkit::LayoutItem(this) );
321 }
322
323 bool LayoutItem::IsLayoutRequested() const
324 {
325   return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
326 }
327
328 void LayoutItem::SetLayoutRequested()
329 {
330   return mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
331 }
332
333 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
334 {
335   DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutBase::SetMeasuredDimensions width(%d) height(%d) \n",
336                                                  MeasureSpec::IntType( measuredWidth.GetSize() ),
337                                                  MeasureSpec::IntType( measuredHeight.GetSize() )
338                );
339
340   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
341   mImpl->mMeasuredWidth = measuredWidth;
342   mImpl->mMeasuredHeight = measuredHeight;
343 }
344
345 LayoutLength LayoutItem::GetMeasuredWidth() const
346 {
347   // Get the size portion of the measured width
348   return  mImpl->mMeasuredWidth.GetSize();
349 }
350
351 LayoutLength LayoutItem::GetMeasuredHeight() const
352 {
353   return  mImpl->mMeasuredHeight.GetSize();
354 }
355
356 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
357 {
358   return mImpl->mMeasuredWidth;
359 }
360
361 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
362 {
363   return mImpl->mMeasuredHeight;
364 }
365
366 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
367 {
368   auto owner = GetOwner();
369   auto actor = Actor::DownCast(owner);
370   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
371
372   return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
373 }
374
375 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
376 {
377   auto owner = GetOwner();
378   auto actor = Actor::DownCast(owner);
379   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
380
381   return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
382 }
383
384 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
385 {
386   auto specMode = measureSpec.GetMode();
387   LayoutLength specSize = measureSpec.GetSize();
388   MeasuredSize result;
389
390   switch( specMode )
391   {
392     case MeasureSpec::Mode::AT_MOST:
393     {
394       if (specSize < size)
395       {
396         result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
397       }
398       else
399       {
400         result.SetSize( size );
401       }
402       break;
403     }
404
405     case MeasureSpec::Mode::EXACTLY:
406     {
407       result.SetSize( specSize );
408       break;
409     }
410
411     case MeasureSpec::Mode::UNSPECIFIED:
412     default:
413     {
414       result.SetSize( size );
415       break;
416     }
417   }
418
419   result.SetState( childMeasuredState );
420   return result;
421 }
422
423
424 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
425 {
426   bool changed = false;
427
428   DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame enter(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
429
430   if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
431   {
432     changed = true;
433
434     auto oldWidth = mImpl->mRight - mImpl->mLeft;
435     auto oldHeight = mImpl->mBottom - mImpl->mTop;
436     auto newWidth = right - left;
437     auto newHeight = bottom - top;
438     bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
439
440     mImpl->mLeft = left;
441     mImpl->mTop = top;
442     mImpl->mRight = right;
443     mImpl->mBottom = bottom;
444
445     mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
446
447
448     // Reflect up to parent control
449     auto owner = GetOwner();
450     auto actor = Actor::DownCast(owner);
451     if( actor )
452     {
453       if( mImpl->mAnimated )
454       {
455         auto animation = Animation::New( 0.5f );
456         animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
457                              Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
458         animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
459                              Vector3( right-left, bottom-top, 0.0f ) );
460         animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
461         animation.Play();
462       }
463       else
464       {
465         // @todo Collate into list of Property & Property::Value pairs.
466         actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
467         actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
468       }
469     }
470
471     if( sizeChanged )
472     {
473       SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
474     }
475   }
476
477   DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame  exit(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
478
479   return changed;
480 }
481
482 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
483 {
484   auto owner = GetOwner();
485   auto actor = Actor::DownCast(owner);
486   if( actor )
487   {
488     actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
489   }
490 }
491
492 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
493 {
494   OnSizeChanged( newSize, oldSize );
495 }
496
497
498 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
499 {
500 }
501
502 void LayoutItem::OnInitialize()
503 {
504 }
505
506
507 } // namespace Internal
508 } // namespace Toolkit
509 } // namespace Dali