Move shadow APIs to Internal::Control::Impl from Internal::Control
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 05f7ab3..c469d1c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
+#include <dali-toolkit/public-api/align-enumerations.h>
 #include <dali-toolkit/public-api/controls/control.h>
+#include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
 #include <dali-toolkit/public-api/styling/style-manager.h>
 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
+#include <dali-toolkit/public-api/visuals/visual-properties.h>
 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
 #include <dali-toolkit/devel-api/controls/control-devel.h>
-#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
+#include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
 #include <dali-toolkit/internal/styling/style-manager-impl.h>
 #include <dali-toolkit/internal/visuals/color/color-visual.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
-#include <dali-toolkit/devel-api/align-enums.h>
 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
 
 namespace Dali
@@ -60,13 +61,75 @@ namespace
 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
 #endif
 
-DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
-DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
-DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
-DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
+/**
+ * @brief Replace the background visual if it's a color visual with the renderIfTransparent property set as required.
+ * @param[in] controlImpl The control implementation
+ * @param[in] renderIfTransaparent Whether we should render if the color is transparent
+ */
+void ChangeBackgroundColorVisual( Control& controlImpl, bool renderIfTransparent )
+{
+  Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
 
-} // unnamed namespace
+  Toolkit::Visual::Base backgroundVisual = controlDataImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
+  if( backgroundVisual )
+  {
+    Property::Map map;
+    backgroundVisual.CreatePropertyMap( map );
+    Property::Value* typeValue = map.Find( Toolkit::Visual::Property::TYPE );
+    if( typeValue && typeValue->Get< int >() == Toolkit::Visual::COLOR )
+    {
+      // Only change it if it's a color visual
+      map[ Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT ] = renderIfTransparent;
+      controlImpl.SetBackground( map );
+    }
+  }
+}
 
+/**
+ * @brief Creates a clipping renderer if required.
+ * (EG. If no renders exist and clipping is enabled).
+ * @param[in] controlImpl The control implementation.
+ */
+void CreateClippingRenderer( Control& controlImpl )
+{
+  // We want to add a transparent background if we do not have one for clipping.
+  Actor self( controlImpl.Self() );
+  int clippingMode = ClippingMode::DISABLED;
+  if( self.GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) )
+  {
+    switch( clippingMode )
+    {
+      case ClippingMode::CLIP_CHILDREN:
+      {
+        if( self.GetRendererCount() == 0u )
+        {
+          Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
+          if( controlDataImpl.mVisuals.Empty() )
+          {
+            controlImpl.SetBackgroundColor( Color::TRANSPARENT );
+          }
+          else
+          {
+            // We have visuals, check if we've set the background and re-create it to
+            // render even if transparent (only if it's a color visual)
+            ChangeBackgroundColorVisual( controlImpl, true );
+          }
+        }
+        break;
+      }
+
+      case ClippingMode::DISABLED:
+      case ClippingMode::CLIP_TO_BOUNDING_BOX:
+      {
+        // If we have a background visual, check if it's a color visual and remove the render if transparent flag
+        ChangeBackgroundColorVisual( controlImpl, false );
+        break;
+      }
+    }
+  }
+}
+
+} // unnamed namespace
 
 
 Toolkit::Control Control::New()
@@ -108,15 +171,18 @@ void Control::SetBackgroundColor( const Vector4& color )
 {
   mImpl->mBackgroundColor = color;
   Property::Map map;
-  map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
+  map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
   map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
 
-  SetBackground( map );
-}
+  int clippingMode = ClippingMode::DISABLED;
+  if( ( Self().GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) ) &&
+      ( clippingMode == ClippingMode::CLIP_CHILDREN ) )
+  {
+    // If clipping-mode is set to CLIP_CHILDREN, then force visual to add the render even if transparent
+    map[ Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT ] = true;
+  }
 
-Vector4 Control::GetBackgroundColor() const
-{
-  return mImpl->mBackgroundColor;
+  SetBackground( map );
 }
 
 void Control::SetBackground( const Property::Map& map )
@@ -132,15 +198,6 @@ void Control::SetBackground( const Property::Map& map )
   }
 }
 
-void Control::SetBackgroundImage( Image image )
-{
-  Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
-  if( visual )
-  {
-    mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
-  }
-}
-
 void Control::ClearBackground()
 {
    mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
@@ -354,13 +411,15 @@ bool Control::EmitKeyEventSignal( const KeyEvent& event )
 
   bool consumed = false;
 
+  consumed = mImpl->FilterKeyEvent( event );
+
   // signals are allocated dynamically when someone connects
-  if ( !mImpl->mKeyEventSignal.Empty() )
+  if ( !consumed && !mImpl->mKeyEventSignal.Empty() )
   {
     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
   }
 
-  if (!consumed)
+  if ( !consumed )
   {
     // Notification for derived classes
     consumed = OnKeyEvent(event);
@@ -498,23 +557,14 @@ void Control::OnStageConnection( int depth )
     }
   }
 
-  if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
-  {
-    Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
-    int clippingMode = ClippingMode::DISABLED;
-    if( clippingValue.Get( clippingMode ) )
-    {
-      // Add a transparent background if we do not have any renderers or visuals so we clip our children
+  // The clipping renderer is only created if required.
+  CreateClippingRenderer( *this );
 
-      if( clippingMode == ClippingMode::CLIP_CHILDREN )
-      {
-        // Create a transparent background visual which will also get staged.
-        SetBackgroundColor( Color::TRANSPARENT );
-      }
-    }
-  }
+  // Request to be laid out when the control is connected to the Stage.
+  // Signal that a Relayout may be needed
 }
 
+
 void Control::OnStageDisconnection()
 {
   mImpl->OnStageDisconnection();
@@ -544,24 +594,12 @@ void Control::OnChildRemove(Actor& child)
 
 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
 {
-  Actor self( Self() );
-  if( index == Actor::Property::CLIPPING_MODE )
+  // If the clipping mode has been set, we may need to create a renderer.
+  // Only do this if we are already on-stage as the OnStageConnection will handle the off-stage clipping controls.
+  if( ( index == Actor::Property::CLIPPING_MODE ) && Self().OnStage() )
   {
-    // Only set the background if we're already on the stage and have no renderers or visuals
-
-    if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
-    {
-      ClippingMode::Type clippingMode = ClippingMode::DISABLED;
-      if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
-      {
-        // Add a transparent background if we do not have one so we clip children
-
-        if( clippingMode == ClippingMode::CLIP_CHILDREN )
-        {
-          SetBackgroundColor( Color::TRANSPARENT );
-        }
-      }
-    }
+    // Note: This method will handle whether creation of the renderer is required.
+    CreateClippingRenderer( *this );
   }
 }
 
@@ -604,7 +642,35 @@ void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
 {
   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
   {
-    container.Add( Self().GetChildAt( i ), size );
+    Actor child = Self().GetChildAt( i );
+    Vector2 newChildSize( size );
+
+    // When set the padding or margin on the control, child should be resized and repositioned.
+    if( ( mImpl->mPadding.start != 0 ) || ( mImpl->mPadding.end != 0 ) || ( mImpl->mPadding.top != 0 ) || ( mImpl->mPadding.bottom != 0 ) ||
+        ( mImpl->mMargin.start != 0 ) || ( mImpl->mMargin.end != 0 ) || ( mImpl->mMargin.top != 0 ) || ( mImpl->mMargin.bottom != 0 ) )
+    {
+      Extents padding = mImpl->mPadding;
+
+      Dali::CustomActor ownerActor(GetOwner());
+      Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( ownerActor.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
+
+      if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
+      {
+        std::swap( padding.start, padding.end );
+      }
+
+      newChildSize.width = size.width - ( padding.start + padding.end );
+      newChildSize.height = size.height - ( padding.top + padding.bottom );
+
+      // Cannot use childs Position property as it can already have padding and margin applied on it,
+      // so we end up cumulatively applying them over and over again.
+      Vector2 childOffset( 0.f, 0.f );
+      childOffset.x += ( mImpl->mMargin.start + padding.start );
+      childOffset.y += ( mImpl->mMargin.top + padding.top );
+
+      child.SetPosition( childOffset.x, childOffset.y );
+    }
+    container.Add( child, newChildSize );
   }
 
   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
@@ -620,11 +686,14 @@ void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dime
 
 Vector3 Control::GetNaturalSize()
 {
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::GetNaturalSize for %s\n", Self().GetName().c_str() );
   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
   if( visual )
   {
     Vector2 naturalSize;
     visual.GetNaturalSize( naturalSize );
+    naturalSize.width += ( mImpl->mPadding.start + mImpl->mPadding.end );
+    naturalSize.height += ( mImpl->mPadding.top + mImpl->mPadding.bottom );
     return Vector3( naturalSize );
   }
   return Vector3::ZERO;