Merge HBoxLayout and VBoxLayout into LinearLayout.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / layouting / linear-layout-impl.cpp
  */
 
 //CLASS HEADER
-#include <dali-toolkit/internal/layouting/hbox-layout-impl.h>
+#include "linear-layout-impl.h"
 
-//EXTERNAL HEADERS
-//INTERNAL HEADERS
+//PUBLIC INCLUDES
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/common/extents.h>
 #include <dali/public-api/actors/actor.h>
+
+//INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/layouting/layout-item.h>
 #include <dali-toolkit/public-api/controls/control-impl.h>
 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
@@ -40,76 +41,96 @@ namespace Toolkit
 namespace Internal
 {
 
-HboxLayoutPtr HboxLayout::New()
+LinearLayoutPtr LinearLayout::New()
 {
-  HboxLayoutPtr layout( new HboxLayout() );
+  LinearLayoutPtr layout( new LinearLayout() );
   return layout;
 }
 
-HboxLayout::HboxLayout()
+LinearLayout::LinearLayout()
 : LayoutGroup(),
   mCellPadding( 0, 0 ),
+  mOrientation( Dali::Toolkit::LinearLayout::Orientation::HORIZONTAL ),
   mTotalLength( 0 )
 {
 }
 
-HboxLayout::~HboxLayout()
+LinearLayout::~LinearLayout()
 {
 }
 
-void HboxLayout::DoInitialize()
+void LinearLayout::SetCellPadding( LayoutSize size )
 {
+  mCellPadding = size;
 }
 
-void HboxLayout::DoRegisterChildProperties( const std::string& containerType )
+LayoutSize LinearLayout::GetCellPadding()
 {
-  auto typeInfo = Dali::TypeRegistry::Get().GetTypeInfo( containerType );
-  if( typeInfo )
-  {
-    Property::IndexContainer indices;
-    typeInfo.GetChildPropertyIndices( indices );
-
-    if( std::find( indices.Begin(), indices.End(), Toolkit::HboxLayout::ChildProperty::WEIGHT ) == indices.End() )
-    {
-      ChildPropertyRegistration( typeInfo.GetName(), "weight",
-                                 Toolkit::HboxLayout::ChildProperty::WEIGHT, Property::FLOAT );
-    }
-  }
+  return mCellPadding;
 }
 
-void HboxLayout::OnChildAdd( LayoutItem& child )
+void LinearLayout::SetOrientation( Dali::Toolkit::LinearLayout::Orientation orientation )
 {
-  auto owner = child.GetOwner();
-  owner.SetProperty( Toolkit::HboxLayout::ChildProperty::WEIGHT, 1.0f );
+  mOrientation = orientation;
 }
 
-void HboxLayout::SetCellPadding( LayoutSize size )
+Dali::Toolkit::LinearLayout::Orientation LinearLayout::GetOrientation()
 {
-  mCellPadding = size;
+  return mOrientation;
 }
 
-LayoutSize HboxLayout::GetCellPadding()
+void LinearLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
 {
-  return mCellPadding;
+#if defined(DEBUG_ENABLED)
+  auto actor = Actor::DownCast(GetOwner());
+
+  std::ostringstream oss;
+  oss << "LinearLayout::OnMeasure  ";
+  if( actor )
+  {
+    oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << "  ";
+  }
+  oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
+  DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
+#endif
+
+  if( mOrientation == Dali::Toolkit::LinearLayout::Orientation::HORIZONTAL )
+  {
+    MeasureHorizontal( widthMeasureSpec, heightMeasureSpec );
+  }
+  else
+  {
+    MeasureVertical( widthMeasureSpec, heightMeasureSpec );
+  }
 }
 
-void HboxLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
+void LinearLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
 {
 #if defined(DEBUG_ENABLED)
   auto actor = Actor::DownCast(GetOwner());
 
   std::ostringstream oss;
-  oss << "HBoxLayout::OnMeasure  ";
+  oss << "LinearLayout::OnLayout  ";
   if( actor )
   {
     oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << "  ";
   }
-  oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
+  oss << "left:" << left << " top:" << top << " right:" << right << " bottom:" << bottom << std::endl;
   DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
 #endif
 
-  DALI_LOG_INFO( gLogFilter, Debug::Concise, "HboxLayout::OnMeasure widthSize(%d) \n", widthMeasureSpec.GetSize());
+  if( mOrientation == Dali::Toolkit::LinearLayout::Orientation::HORIZONTAL )
+  {
+    LayoutHorizontal( left, top, right, bottom );
+  }
+  else
+  {
+    LayoutVertical( left, top, right, bottom );
+  }
+}
 
+void LinearLayout::MeasureHorizontal( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
+{
   auto widthMode = widthMeasureSpec.GetMode();
   auto heightMode = heightMeasureSpec.GetMode();
   bool isExactly = (widthMode == MeasureSpec::Mode::EXACTLY);
@@ -136,7 +157,7 @@ void HboxLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeas
       auto childWidth = childLayout->GetMeasuredWidth();
       auto childMargin = childLayout->GetMargin();
 
-      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "HboxLayout::OnMeasure childWidth(%d)\n", MeasureSpec::IntType( childWidth ) );
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LinearLayout::OnMeasure childWidth(%d)\n", MeasureSpec::IntType( childWidth ) );
 
       auto length = childWidth + LayoutLength::IntType(childMargin.start + childMargin.end);
 
@@ -203,7 +224,7 @@ void HboxLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeas
   }
 }
 
-void HboxLayout::ForceUniformHeight( int count, MeasureSpec widthMeasureSpec )
+void LinearLayout::ForceUniformHeight( int count, MeasureSpec widthMeasureSpec )
 {
   // Pretend that the linear layout has an exact size. This is the measured height of
   // ourselves. The measured height should be the max height of the children, changed
@@ -233,7 +254,7 @@ void HboxLayout::ForceUniformHeight( int count, MeasureSpec widthMeasureSpec )
   }
 }
 
-void HboxLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
+void LinearLayout::LayoutHorizontal( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
 {
   auto owner = GetOwner();
   auto actor = Actor::DownCast(owner);
@@ -271,7 +292,6 @@ void HboxLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, La
     {
       auto childWidth = childLayout->GetMeasuredWidth();
       auto childHeight = childLayout->GetMeasuredHeight();
-
       auto childMargin = childLayout->GetMargin();
 
       childTop = LayoutLength(padding.top) + ((childSpace - childHeight) / 2) + childMargin.top - childMargin.bottom;
@@ -283,6 +303,150 @@ void HboxLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, La
   }
 }
 
+void LinearLayout::MeasureVertical( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
+{
+  auto widthMode = widthMeasureSpec.GetMode();
+
+  bool matchWidth = false;
+  bool allFillParent = true;
+  LayoutLength maxWidth = 0;
+  LayoutLength alternativeMaxWidth = 0;
+
+  struct
+  {
+    MeasuredSize::State widthState;
+    MeasuredSize::State heightState;
+  } childState = { MeasuredSize::State::MEASURED_SIZE_OK, MeasuredSize::State::MEASURED_SIZE_OK };
+
+  // measure children, and determine if further resolution is required
+  for( unsigned int i=0; i<GetChildCount(); ++i )
+  {
+    auto childLayout = GetChildAt( i );
+    if( childLayout )
+    {
+      auto childOwner = childLayout->GetOwner();
+      auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
+
+      MeasureChildWithMargins( childLayout, widthMeasureSpec, 0, heightMeasureSpec, 0 );
+      auto childHeight = childLayout->GetMeasuredHeight();
+      auto childMargin = childLayout->GetMargin();
+      auto length = childHeight + LayoutLength::IntType(childMargin.top + childMargin.bottom );
+
+      auto cellPadding = i<GetChildCount()-1 ? mCellPadding.height : 0;
+      auto totalLength = mTotalLength;
+      mTotalLength = std::max( totalLength, totalLength + length + cellPadding);
+
+      bool matchWidthLocally = false;
+      if( widthMode != MeasureSpec::Mode::EXACTLY && desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT )
+      {
+        // Will have to re-measure at least this child when we know exact height.
+        matchWidth = true;
+        matchWidthLocally = true;
+      }
+
+      auto marginWidth = LayoutLength( childMargin.start + childMargin.end );
+      auto childWidth = childLayout->GetMeasuredWidth() + marginWidth;
+
+      // was combineMeasuredStates()
+      if( childLayout->GetMeasuredWidthAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
+      {
+        childState.widthState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
+      }
+      if( childLayout->GetMeasuredHeightAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
+      {
+        childState.heightState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
+      }
+
+      maxWidth = std::max( maxWidth, childWidth );
+      allFillParent = ( allFillParent && desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT );
+      alternativeMaxWidth = std::max( alternativeMaxWidth, matchWidthLocally ? marginWidth : childWidth );
+    }
+  }
+  Extents padding = GetPadding();
+  mTotalLength += padding.top + padding.bottom;
+  auto heightSize = mTotalLength;
+  heightSize = std::max( heightSize, GetSuggestedMinimumHeight() );
+  MeasuredSize heightSizeAndState = ResolveSizeAndState( heightSize, heightMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
+  heightSize = heightSizeAndState.GetSize();
+
+  if( !allFillParent && widthMode != MeasureSpec::Mode::EXACTLY )
+  {
+    maxWidth = alternativeMaxWidth;
+  }
+  maxWidth += padding.start + padding.end;
+  maxWidth = std::max( maxWidth, GetSuggestedMinimumWidth() );
+
+  heightSizeAndState.SetState( childState.heightState );
+
+  SetMeasuredDimensions( ResolveSizeAndState( maxWidth, widthMeasureSpec, childState.widthState ),
+                         heightSizeAndState );
+
+  if( matchWidth )
+  {
+    ForceUniformWidth( GetChildCount(), heightMeasureSpec );
+  }
+}
+
+void LinearLayout::ForceUniformWidth( int count, MeasureSpec heightMeasureSpec )
+{
+  // Pretend that the linear layout has an exact size.
+  auto uniformMeasureSpec = MeasureSpec( GetMeasuredWidth(), MeasureSpec::Mode::EXACTLY );
+  for (int i = 0; i < count; ++i)
+  {
+    LayoutItemPtr childLayout = GetChildAt(i);
+    if( childLayout != nullptr )
+    {
+      auto childOwner = childLayout->GetOwner();
+      auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
+      auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
+
+      if( desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT )
+      {
+        // Temporarily force children to reuse their old measured height
+        int oldHeight = desiredHeight;
+        childOwner.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, childLayout->GetMeasuredHeight().mValue );
+
+        // Remeasure with new dimensions
+        MeasureChildWithMargins( childLayout, uniformMeasureSpec, 0, heightMeasureSpec, 0 );
+
+        childOwner.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, oldHeight );
+      }
+    }
+  }
+}
+
+void LinearLayout::LayoutVertical( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
+{
+  Extents padding = GetPadding();
+
+  LayoutLength childTop( 0 );
+  LayoutLength childLeft( padding.start );
+
+  // Where bottom of child should go
+  auto width = right - left;
+
+  // Space available for child
+  auto childSpace = width - padding.start - padding.end;
+  auto count = GetChildCount();
+
+  for( unsigned int childIndex = 0; childIndex < count; childIndex++)
+  {
+    LayoutItemPtr childLayout = GetChildAt( childIndex );
+    if( childLayout != nullptr )
+    {
+      auto childWidth = childLayout->GetMeasuredWidth();
+      auto childHeight = childLayout->GetMeasuredHeight();
+      auto childMargin = childLayout->GetMargin();
+
+      childTop += childMargin.top;
+      childLeft = ( childSpace - childWidth ) / 2 + childMargin.start - childMargin.end;
+
+      childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
+      childTop += childHeight + childMargin.bottom + mCellPadding.height;
+    }
+  }
+}
+
 } // namespace Internal
 } // namespace Toolkit
 } // namespace Dali