Merge "Fix some SVACE issues for yoga third-party code." 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/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     //resolveRtlPropertiesIfNeeded();
145
146     int cacheIndex = -1;  // = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
147     if( cacheIndex < 0 ) //|| sIgnoreMeasureCache )
148     {
149       // measure ourselves, this should set the measured dimension flag back
150       OnMeasure( widthMeasureSpec, heightMeasureSpec );
151       mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
152     }
153     else
154     {
155       mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
156     }
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   //mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 | (long) mMeasuredHeight & 0xffffffffL); // suppress sign extension
168 }
169
170 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
171 {
172   if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
173   {
174     OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
175     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
176   }
177
178   bool changed = SetFrame( l, t, r, b );
179
180   if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
181   {
182     OnLayout( changed, l, t, r, b );
183     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
184   }
185
186   mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
187   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
188 }
189
190 LayoutLength LayoutItem::GetMinimumWidth() const
191 {
192   return mImpl->mMinimumSize.GetWidth();
193 }
194
195 LayoutLength LayoutItem::GetMinimumHeight() const
196 {
197   return mImpl->mMinimumSize.GetHeight();
198 }
199
200 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
201 {
202   mImpl->mMinimumSize.SetWidth( minimumWidth );
203   RequestLayout();
204 }
205
206 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
207 {
208   mImpl->mMinimumSize.SetHeight( minimumHeight );
209   RequestLayout();
210 }
211
212 Extents LayoutItem::GetPadding() const
213 {
214   return mImpl->mPadding;
215 }
216
217 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
218 {
219   LayoutLength result = size;
220   auto specMode = measureSpec.GetMode();
221   auto specSize = measureSpec.GetSize();
222
223   switch (specMode)
224   {
225     case MeasureSpec::Mode::UNSPECIFIED:
226     {
227       result = size;
228       break;
229     }
230     case MeasureSpec::Mode::AT_MOST:
231     case MeasureSpec::Mode::EXACTLY:
232     {
233       result = specSize;
234       break;
235     }
236   }
237   return result;
238 }
239
240 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
241 {
242   SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
243                          GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
244 }
245
246 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
247 {
248 }
249
250 LayoutParent* LayoutItem::GetParent()
251 {
252   return mImpl->mLayoutParent;
253 }
254
255 void LayoutItem::RequestLayout()
256 {
257   // @todo Enforce failure if called in Measure/Layout passes.
258   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
259   Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
260   layoutController.RequestLayout( Toolkit::LayoutItem(this) );
261 }
262
263 bool LayoutItem::IsLayoutRequested() const
264 {
265   return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
266 }
267
268 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
269 {
270   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
271   mImpl->mMeasuredWidth = measuredWidth;
272   mImpl->mMeasuredHeight = measuredHeight;
273 }
274
275 LayoutLength LayoutItem::GetMeasuredWidth() const
276 {
277   // Get the size portion of the measured width
278   return  mImpl->mMeasuredWidth.GetSize();
279 }
280
281 LayoutLength LayoutItem::GetMeasuredHeight() const
282 {
283   return  mImpl->mMeasuredHeight.GetSize();
284 }
285
286 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
287 {
288   return mImpl->mMeasuredWidth;
289 }
290
291 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
292 {
293   return mImpl->mMeasuredHeight;
294 }
295
296 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
297 {
298   auto owner = GetOwner();
299   auto actor = Actor::DownCast(owner);
300   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
301
302   return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
303 }
304
305 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
306 {
307   auto owner = GetOwner();
308   auto actor = Actor::DownCast(owner);
309   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
310
311   return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
312 }
313
314 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
315 {
316   auto specMode = measureSpec.GetMode();
317   LayoutLength specSize = measureSpec.GetSize();
318   MeasuredSize result;
319
320   switch( specMode )
321   {
322     case MeasureSpec::Mode::AT_MOST:
323     {
324       if (specSize < size)
325       {
326         result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
327       }
328       else
329       {
330         result.SetSize( size );
331       }
332       break;
333     }
334
335     case MeasureSpec::Mode::EXACTLY:
336     {
337       result.SetSize( specSize );
338       break;
339     }
340
341     case MeasureSpec::Mode::UNSPECIFIED:
342     default:
343     {
344       result.SetSize( size );
345       break;
346     }
347   }
348
349   result.SetState( childMeasuredState );
350   return result;
351 }
352
353
354 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
355 {
356   bool changed = false;
357
358   DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
359
360   if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
361   {
362     changed = true;
363
364     auto oldWidth = mImpl->mRight - mImpl->mLeft;
365     auto oldHeight = mImpl->mBottom - mImpl->mTop;
366     auto newWidth = right - left;
367     auto newHeight = bottom - top;
368     bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
369
370     mImpl->mLeft = left;
371     mImpl->mTop = top;
372     mImpl->mRight = right;
373     mImpl->mBottom = bottom;
374
375     mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
376
377
378     // Reflect up to parent control
379     auto owner = GetOwner();
380     auto actor = Actor::DownCast(owner);
381     if( actor )
382     {
383       if( mImpl->mAnimated )
384       {
385         auto animation = Animation::New( 0.5f );
386         animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
387                              Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
388         animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
389                              Vector3( right-left, bottom-top, 0.0f ) );
390         animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
391         animation.Play();
392       }
393       else
394       {
395         // @todo Collate into list of Property & Property::Value pairs.
396         actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
397         actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
398       }
399     }
400
401     if( sizeChanged )
402     {
403       SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
404     }
405   }
406   return changed;
407 }
408
409 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
410 {
411   auto owner = GetOwner();
412   auto actor = Actor::DownCast(owner);
413   if( actor )
414   {
415     actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
416   }
417 }
418
419 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
420 {
421   OnSizeChanged( newSize, oldSize );
422 }
423
424
425 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
426 {
427 }
428
429 void LayoutItem::OnInitialize()
430 {
431 }
432
433
434 } // namespace Internal
435 } // namespace Toolkit
436 } // namespace Dali