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