Add Layout complex animation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / control / control-data-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index e1d82e3..fcfb147
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
@@ -19,7 +19,7 @@
 #include "control-data-impl.h"
 
 // EXTERNAL INCLUDES
-#include <dali/public-api/common/dali-common.h>
+#include <dali-toolkit/public-api/dali-toolkit-common.h>
 #include <dali/integration-api/debug.h>
 #include <dali/devel-api/object/handle-devel.h>
 #include <dali/devel-api/scripting/enum-helper.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
-#include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
-#include <dali-toolkit/internal/styling/style-manager-impl.h>
 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
-#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
-#include <dali-toolkit/internal/visuals/visual-string-constants.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/controls/control-wrapper-impl.h>
+#include <dali-toolkit/devel-api/layouting/layout-item.h>
+#include <dali-toolkit/internal/styling/style-manager-impl.h>
+#include <dali-toolkit/internal/visuals/visual-string-constants.h>
 
 namespace Dali
 {
@@ -67,6 +68,7 @@ namespace
 
 #if defined(DEBUG_ENABLED)
 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
+Debug::Filter* gLogFilterLayout = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT");
 #endif
 
 
@@ -85,10 +87,10 @@ void Remove( DictionaryKeys& keys, const std::string& name )
   }
 }
 
-Toolkit::DevelVisual::Type GetVisualTypeFromMap( const Property::Map& map )
+Toolkit::Visual::Type GetVisualTypeFromMap( const Property::Map& map )
 {
-  Property::Value* typeValue = map.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE  );
-  Toolkit::DevelVisual::Type type = Toolkit::DevelVisual::IMAGE;
+  Property::Value* typeValue = map.Find( Toolkit::Visual::Property::TYPE, VISUAL_TYPE  );
+  Toolkit::Visual::Type type = Toolkit::Visual::IMAGE;
   if( typeValue )
   {
     Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, type );
@@ -275,6 +277,24 @@ TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &Do
 
 DALI_TYPE_REGISTRATION_END()
 
+/**
+ * @brief Iterate through given container and setOffStage any visual found
+ *
+ * @param[in] container Container of visuals
+ * @param[in] parent Parent actor to remove visuals from
+ */
+void SetVisualsOffStage( const RegisteredVisualContainer& container, Actor parent )
+{
+  for( auto iter = container.Begin(), end = container.End() ; iter!= end; iter++)
+  {
+    if( (*iter)->visual )
+    {
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::SetOffStage Setting visual(%d) off stage\n", (*iter)->index );
+      Toolkit::GetImplementation((*iter)->visual).SetOffStage( parent );
+    }
+  }
+}
+
 } // unnamed namespace
 
 
@@ -284,20 +304,22 @@ const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgro
 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage",        Toolkit::Control::Property::BACKGROUND_IMAGE,             Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus",          Toolkit::Control::Property::KEY_INPUT_FOCUS,              Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background",             Toolkit::Control::Property::BACKGROUND,                   Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip",                Toolkit::DevelControl::Property::TOOLTIP,                 Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-const PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "state",                  Toolkit::DevelControl::Property::STATE,                   Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-const PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "subState",               Toolkit::DevelControl::Property::SUB_STATE,               Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-const PropertyRegistration Control::Impl::PROPERTY_9( typeRegistration, "leftFocusableActorId",   Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-const PropertyRegistration Control::Impl::PROPERTY_10( typeRegistration, "rightFocusableActorId", Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID,Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-const PropertyRegistration Control::Impl::PROPERTY_11( typeRegistration, "upFocusableActorId",    Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID,   Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-const PropertyRegistration Control::Impl::PROPERTY_12( typeRegistration, "downFocusableActorId",  Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
-
+const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "margin",                 Toolkit::Control::Property::MARGIN,                       Property::EXTENTS, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "padding",                Toolkit::Control::Property::PADDING,                      Property::EXTENTS, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "tooltip",                Toolkit::DevelControl::Property::TOOLTIP,                 Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_9( typeRegistration, "state",                  Toolkit::DevelControl::Property::STATE,                   Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_10( typeRegistration, "subState",               Toolkit::DevelControl::Property::SUB_STATE,               Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_11( typeRegistration, "leftFocusableActorId",   Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_12( typeRegistration, "rightFocusableActorId", Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID,Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_13( typeRegistration, "upFocusableActorId",    Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID,   Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
+const PropertyRegistration Control::Impl::PROPERTY_14( typeRegistration, "downFocusableActorId",  Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
 
 
 Control::Impl::Impl( Control& controlImpl )
 : mControlImpl( controlImpl ),
   mState( Toolkit::DevelControl::NORMAL ),
   mSubStateName(""),
+  mLayout( NULL ),
   mLeftFocusableActorId( -1 ),
   mRightFocusableActorId( -1 ),
   mUpFocusableActorId( -1 ),
@@ -305,16 +327,23 @@ Control::Impl::Impl( Control& controlImpl )
   mStyleName(""),
   mBackgroundColor(Color::TRANSPARENT),
   mStartingPinchScale( NULL ),
+  mMargin( 0, 0, 0, 0 ),
+  mPadding( 0, 0, 0, 0 ),
   mKeyEventSignal(),
+  mKeyInputFocusGainedSignal(),
+  mKeyInputFocusLostSignal(),
+  mResourceReadySignal(),
   mPinchGestureDetector(),
   mPanGestureDetector(),
   mTapGestureDetector(),
   mLongPressGestureDetector(),
+  mTooltip( NULL ),
+  mInputMethodContext(),
   mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
   mIsKeyboardNavigationSupported( false ),
-  mIsKeyboardFocusGroup( false )
+  mIsKeyboardFocusGroup( false ),
+  mIsLayoutingRequired( false )
 {
-
 }
 
 Control::Impl::~Impl()
@@ -354,89 +383,6 @@ void Control::Impl::LongPressDetected(Actor actor, const LongPressGesture& longP
   mControlImpl.OnLongPress(longPress);
 }
 
-// Called by a Visual when it's resource is ready
-void Control::Impl::ResourceReady( Visual::Base& object)
-{
-  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady \n");
-
-  // A resource is ready, check if is in the replacement visual container
-  // Iterate through all visuals in replacement container and store indexes of ready visuals
-  Dali::Vector <Property::Index> readyVisuals;
-  Actor self = mControlImpl.Self();
-
-  for( auto replacementVisualIter = mReplacementVisuals.Begin();
-        replacementVisualIter < mReplacementVisuals.End(); ++replacementVisualIter )
-  {
-    const Toolkit::Visual::Base replacementVisual = (*replacementVisualIter)->visual;
-    const Internal::Visual::Base& replacementVisualImpl = Toolkit::GetImplementation( replacementVisual );
-
-    if( replacementVisualImpl.IsResourceReady() )
-    {
-      // Check if new replacement visual (index) is already queued for replacement and swap old for new.
-      RegisteredVisualContainer::Iterator registeredVisualsIter;
-      if( FindVisual( (*replacementVisualIter)->index, mVisuals, registeredVisualsIter ) )
-      {
-        Property::Index readyVisualIndex = (*replacementVisualIter)->index;
-        DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady: %d Ready to replace\n", readyVisualIndex );
-        readyVisuals.PushBack( readyVisualIndex );
-        // Remove current shown visual from stage and from registered visuals container
-        Toolkit::GetImplementation((*registeredVisualsIter)->visual).SetOffStage( self );
-        mVisuals.Erase( registeredVisualsIter );
-      }
-    }
-  }
-
-  for( auto readyVisualsIter = readyVisuals.Begin(); readyVisualsIter != readyVisuals.End(); readyVisualsIter++ )
-  {
-    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady: %d Matched\n", (*readyVisualsIter) );
-    // Move new visual to be shown from replacement container into the control's registered visuals container
-    // Replacement visual has already been set on stage when it was added to replacement container
-    RegisteredVisualContainer::Iterator readyReplacementVisual;
-    if( FindVisual( (*readyVisualsIter) , mReplacementVisuals, readyReplacementVisual ) )
-    {
-      MoveVisual( readyReplacementVisual, mReplacementVisuals, mVisuals ); // Erases visual from replacement queue
-    }
-    // A visual has been replaced so control will most likely need relayouting
-    mControlImpl.RelayoutRequest();
-  }
-
-  // go through and check if all the visuals are ready, if they are emit a signal
-  for( auto visualIter = mVisuals.Begin();
-        visualIter != mVisuals.End(); ++visualIter )
-  {
-    const Toolkit::Visual::Base visual = (*visualIter)->visual;
-    const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
-
-    // one of the visuals is not ready
-    if( !visualImpl.IsResourceReady() )
-    {
-      return;
-    }
-  }
-
-  // all the visuals are ready
-  Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
-  mResourceReadySignal.Emit( handle );
-}
-
-bool Control::Impl::IsResourceReady() const
-{
-  // go through and check all the visuals are ready
-  for ( RegisteredVisualContainer::ConstIterator visualIter = mVisuals.Begin();
-         visualIter != mVisuals.End(); ++visualIter )
-   {
-     const Toolkit::Visual::Base visual = (*visualIter)->visual;
-     const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
-
-     // one of the visuals is not ready
-     if( !visualImpl.IsResourceReady()  )
-     {
-       return false;
-     }
-   }
-  return true;
-}
-
 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
 {
   RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::NOT_SET );
@@ -464,48 +410,61 @@ void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base
   bool visualReplaced ( false );
   Actor self = mControlImpl.Self();
 
+  // Set the depth index, if not set by caller this will be either the current visual depth, max depth of all visuals
+  // or zero.
+  int requiredDepthIndex = visual.GetDepthIndex();
+
+  if( depthIndexValueSet == DepthIndexValue::SET )
+  {
+    requiredDepthIndex = depthIndex;
+  }
+
+  // Visual replacement, existing visual should only be removed from stage when replacement ready.
   if( !mVisuals.Empty() )
   {
     RegisteredVisualContainer::Iterator registeredVisualsiter;
-    // Check if visual (index) is already registered.  Replace if so.
+    // Check if visual (index) is already registered, this is the current visual.
     if( FindVisual( index, mVisuals, registeredVisualsiter ) )
     {
-      if( (*registeredVisualsiter)->visual )
+      Toolkit::Visual::Base& currentRegisteredVisual = (*registeredVisualsiter)->visual;
+      if( currentRegisteredVisual )
       {
         // Store current visual depth index as may need to set the replacement visual to same depth
         const int currentDepthIndex = (*registeredVisualsiter)->visual.GetDepthIndex();
 
-        // Monitor when the visuals resources are ready
-        StopObservingVisual( (*registeredVisualsiter)->visual );
-        StartObservingVisual( visual );
+        // No longer required to know if the replaced visual's resources are ready
+        StopObservingVisual( currentRegisteredVisual );
 
-        if(  self.OnStage() )
+        // If control staged and visual enabled then visuals will be swapped once ready
+        if(  self.OnStage() && enabled )
         {
-          DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Adding visual to replacement Queue: %d \n", index );
-          // Check if visual is currently in the process of being replaced
-          RegisteredVisualContainer::Iterator queuedReplacementVisual;
-          if ( FindVisual( index, mReplacementVisuals, queuedReplacementVisual ) )
+          // Check if visual is currently in the process of being replaced ( is in removal container )
+          RegisteredVisualContainer::Iterator visualQueuedForRemoval;
+          if ( FindVisual( index, mRemoveVisuals, visualQueuedForRemoval ) )
           {
-            // If visual on replacement queue is going to be replaced before it's ready then will be removed from queue (and stage)
-            // Only the the last requested visual will be queued and then displayed.
-            Toolkit::GetImplementation( (*queuedReplacementVisual)->visual ).SetOffStage( self );
-            mReplacementVisuals.Erase(queuedReplacementVisual);
+            // Visual with same index is already in removal container so current visual pending
+            // Only the the last requested visual will be displayed so remove current visual which is staged but not ready.
+            Toolkit::GetImplementation( currentRegisteredVisual ).SetOffStage( self );
+            mVisuals.Erase( registeredVisualsiter );
+          }
+          else
+          {
+            // current visual not already in removal container so add now.
+            DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Move current registered visual to removal Queue: %d \n", index );
+            MoveVisual( registeredVisualsiter, mVisuals, mRemoveVisuals );
           }
-          // Add to replacement list
-          mReplacementVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
         }
         else
         {
-          // Not staged so can just replace registered visual
-          (*registeredVisualsiter)->visual = visual;
-          (*registeredVisualsiter)->enabled = ( enabled == VisualState::ENABLED ) ? true : false;
+          // Control not staged or visual disabled so can just erase from registered visuals and new visual will be added later.
+          mVisuals.Erase( registeredVisualsiter );
         }
 
         // If we've not set the depth-index value and the new visual does not have a depth index applied to it, then use the previously set depth-index for this index
         if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
             ( visual.GetDepthIndex() == 0 ) )
         {
-          visual.SetDepthIndex( currentDepthIndex );
+          requiredDepthIndex = currentDepthIndex;
         }
       }
 
@@ -535,15 +494,9 @@ void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base
 
   if( !visualReplaced ) // New registration entry
   {
-    DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration %d\n", index);
-    mVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
-
-    // monitor when the visuals resources are ready
-    StartObservingVisual( visual );
-
     // If we've not set the depth-index value, we have more than one visual and the visual does not have a depth index, then set it to be the highest
     if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
-        ( mVisuals.Size() > 1 ) &&
+        ( mVisuals.Size() > 0 ) &&
         ( visual.GetDepthIndex() == 0 ) )
     {
       int maxDepthIndex = std::numeric_limits< int >::min();
@@ -558,44 +511,63 @@ void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base
           maxDepthIndex = visualDepthIndex;
         }
       }
-
       ++maxDepthIndex; // Add one to the current maximum depth index so that our added visual appears on top
-      visual.SetDepthIndex( maxDepthIndex );
+      requiredDepthIndex = std::max( 0, maxDepthIndex ); // Start at zero if maxDepth index belongs to a background
     }
   }
 
   if( visual )
   {
-    // If the caller has set the depth-index, then set it here
-    if( depthIndexValueSet == DepthIndexValue::SET )
-    {
-      visual.SetDepthIndex( depthIndex );
-    }
+    // Set determined depth index
+    visual.SetDepthIndex( requiredDepthIndex );
 
+    // Monitor when the visual resources are ready
+    StartObservingVisual( visual );
+
+    DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration index[%d] depth[%d]\n", index, requiredDepthIndex );
+    RegisteredVisual* newRegisteredVisual  = new RegisteredVisual( index, visual,
+                                             ( enabled == VisualState::ENABLED ? true : false ),
+                                             ( visualReplaced && enabled ) ) ;
+    mVisuals.PushBack( newRegisteredVisual );
+
+    Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
     // Put on stage if enabled and the control is already on the stage
     if( ( enabled == VisualState::ENABLED ) && self.OnStage() )
     {
-      // Visual must be set on stage for the renderer to be created and the ResourceReady triggered.
-      Toolkit::GetImplementation(visual).SetOnStage( self );
+      visualImpl.SetOnStage( self );
+    }
+    else if( visualImpl.IsResourceReady() ) // When not being staged, check if visual already 'ResourceReady' before it was Registered. ( Resource may have been loaded already )
+    {
+      ResourceReady( visualImpl );
     }
+
   }
 
-  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n",  visual.GetName().c_str(), index, enabled?"T":"F" );
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n",  visual.GetName().c_str(), index, enabled?"true":"false" );
 }
 
 void Control::Impl::UnregisterVisual( Property::Index index )
 {
-   RegisteredVisualContainer::Iterator iter;
-   if ( FindVisual( index, mVisuals, iter ) )
-   {
-     // stop observing visual
-     StopObservingVisual( (*iter)->visual );
+  RegisteredVisualContainer::Iterator iter;
+  if ( FindVisual( index, mVisuals, iter ) )
+  {
+    // stop observing visual
+    StopObservingVisual( (*iter)->visual );
 
-     Actor self( mControlImpl.Self() );
-     Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
-     (*iter)->visual.Reset();
-     mVisuals.Erase( iter );
-   }
+    Actor self( mControlImpl.Self() );
+    Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
+    (*iter)->visual.Reset();
+    mVisuals.Erase( iter );
+  }
+
+  if( FindVisual( index, mRemoveVisuals, iter ) )
+  {
+    Actor self( mControlImpl.Self() );
+    Toolkit::GetImplementation( (*iter)->visual ).SetOffStage( self );
+    (*iter)->pending = false;
+    (*iter)->visual.Reset();
+    mRemoveVisuals.Erase( iter );
+  }
 }
 
 Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
@@ -611,7 +583,7 @@ Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
 
 void Control::Impl::EnableVisual( Property::Index index, bool enable )
 {
-  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual (%d)\n", index);
+  DALI_LOG_INFO( gLogFilter, Debug::General, "Control::EnableVisual(%d, %s)\n", index, enable?"T":"F");
 
   RegisteredVisualContainer::Iterator iter;
   if ( FindVisual( index, mVisuals, iter ) )
@@ -638,6 +610,10 @@ void Control::Impl::EnableVisual( Property::Index index, bool enable )
       }
     }
   }
+  else
+  {
+    DALI_LOG_WARNING( "Control::EnableVisual(%d, %s) FAILED - NO SUCH VISUAL\n", index, enable?"T":"F" );
+  }
 }
 
 bool Control::Impl::IsVisualEnabled( Property::Index index ) const
@@ -666,82 +642,175 @@ void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
   visualImpl.AddResourceObserver( *this );
 }
 
-Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
+// Called by a Visual when it's resource is ready
+void Control::Impl::ResourceReady( Visual::Base& object)
 {
-  Dali::Animation transition;
-  const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::Impl::ResourceReady() replacements pending[%d]\n", mRemoveVisuals.Count() );
 
-  if( transitionData.Count() > 0 )
+  Actor self = mControlImpl.Self();
+
+  // A resource is ready, find resource in the registered visuals container and get its index
+  for( auto registeredIter = mVisuals.Begin(),  end = mVisuals.End(); registeredIter != end; ++registeredIter )
   {
-    // Setup a Transition from TransitionData.
-    TransitionData::Iterator end = transitionData.End();
-    for( TransitionData::Iterator iter = transitionData.Begin() ;
-         iter != end; ++iter )
+    Internal::Visual::Base& registeredVisualImpl = Toolkit::GetImplementation( (*registeredIter)->visual );
+
+    if( &object == &registeredVisualImpl )
     {
-      TransitionData::Animator* animator = (*iter);
+      RegisteredVisualContainer::Iterator visualToRemoveIter;
+      // Find visual with the same index in the removal container
+      // Set if off stage as it's replacement is now ready.
+      // Remove if from removal list as now removed from stage.
+      // Set Pending flag on the ready visual to false as now ready.
+      if( FindVisual( (*registeredIter)->index, mRemoveVisuals, visualToRemoveIter ) )
+      {
+        (*registeredIter)->pending = false;
+        Toolkit::GetImplementation( (*visualToRemoveIter)->visual ).SetOffStage( self );
+        mRemoveVisuals.Erase( visualToRemoveIter );
+      }
+      break;
+    }
+  }
+
+  // A visual is ready so control may need relayouting if staged
+  if ( self.OnStage() )
+  {
+    mControlImpl.RelayoutRequest();
+  }
 
-      Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
+  // Emit signal if all enabled visuals registered by the control are ready.
+  if( IsResourceReady() )
+  {
+    Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
+    mResourceReadySignal.Emit( handle );
+  }
+}
 
-      if( visual )
-      {
+bool Control::Impl::IsResourceReady() const
+{
+  // Iterate through and check all the enabled visuals are ready
+  for( auto visualIter = mVisuals.Begin();
+         visualIter != mVisuals.End(); ++visualIter )
+  {
+    const Toolkit::Visual::Base visual = (*visualIter)->visual;
+    const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+
+    // one of the enabled visuals is not ready
+    if( !visualImpl.IsResourceReady() && (*visualIter)->enabled )
+    {
+      return false;
+    }
+  }
+  return true;
+}
+
+Toolkit::Visual::ResourceStatus Control::Impl::GetVisualResourceStatus( Property::Index index ) const
+{
+  RegisteredVisualContainer::Iterator iter;
+  if ( FindVisual( index, mVisuals, iter ) )
+  {
+    const Toolkit::Visual::Base visual = (*iter)->visual;
+    const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+    return visualImpl.GetResourceStatus( );
+  }
+
+  return Toolkit::Visual::ResourceStatus::PREPARING;
+}
+
+
+
+void Control::Impl::AddTransitions( Dali::Animation& animation,
+                                    const Toolkit::TransitionData& handle,
+                                    bool createAnimation )
+{
+  // Setup a Transition from TransitionData.
+  const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
+  TransitionData::Iterator end = transitionData.End();
+  for( TransitionData::Iterator iter = transitionData.Begin() ;
+       iter != end; ++iter )
+  {
+    TransitionData::Animator* animator = (*iter);
+
+    Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
+
+    if( visual )
+    {
 #if defined(DEBUG_ENABLED)
-        Dali::TypeInfo typeInfo;
-        ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
-        if( controlWrapperImpl )
-        {
-          typeInfo = controlWrapperImpl->GetTypeInfo();
-        }
+      Dali::TypeInfo typeInfo;
+      ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
+      if( controlWrapperImpl )
+      {
+        typeInfo = controlWrapperImpl->GetTypeInfo();
+      }
 
-        DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
-                       visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
+      DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
+                     visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
 #endif
-        Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
-        visualImpl.AnimateProperty( transition, *animator );
-      }
-      else
+      Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+      visualImpl.AnimateProperty( animation, *animator );
+    }
+    else
+    {
+      DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
+      // Otherwise, try any actor children of control (Including the control)
+      Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
+      if( child )
       {
-        DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
-        // Otherwise, try any actor children of control (Including the control)
-        Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
-        if( child )
+        Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
+        if( propertyIndex != Property::INVALID_INDEX )
         {
-          Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
-          if( propertyIndex != Property::INVALID_INDEX )
+          if( animator->animate == false )
           {
-            if( animator->animate == false )
+            if( animator->targetValue.GetType() != Property::NONE )
             {
-              if( animator->targetValue.GetType() != Property::NONE )
-              {
-                child.SetProperty( propertyIndex, animator->targetValue );
-              }
+              child.SetProperty( propertyIndex, animator->targetValue );
             }
-            else // animate the property
+          }
+          else // animate the property
+          {
+            if( animator->initialValue.GetType() != Property::NONE )
+            {
+              child.SetProperty( propertyIndex, animator->initialValue );
+            }
+
+            if( createAnimation && !animation )
             {
-              if( animator->initialValue.GetType() != Property::NONE )
-              {
-                child.SetProperty( propertyIndex, animator->initialValue );
-              }
-
-              if( ! transition )
-              {
-                transition = Dali::Animation::New( 0.1f );
-              }
-
-              transition.AnimateTo( Property( child, propertyIndex ),
-                                    animator->targetValue,
-                                    animator->alphaFunction,
-                                    TimePeriod( animator->timePeriodDelay,
-                                                animator->timePeriodDuration ) );
+              animation = Dali::Animation::New( 0.1f );
             }
+
+            animation.AnimateTo( Property( child, propertyIndex ),
+                                 animator->targetValue,
+                                 animator->alphaFunction,
+                                 TimePeriod( animator->timePeriodDelay,
+                                             animator->timePeriodDuration ) );
           }
         }
       }
     }
   }
+}
 
+Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& transitionData )
+{
+  Dali::Animation transition;
+
+  if( transitionData.Count() > 0 )
+  {
+    AddTransitions( transition, transitionData, true );
+  }
   return transition;
 }
 
+
+
+void Control::Impl::DoAction( Dali::Property::Index visualIndex, Dali::Property::Index actionId, const Dali::Property::Value attributes )
+{
+  RegisteredVisualContainer::Iterator iter;
+  if ( FindVisual( visualIndex, mVisuals, iter ) )
+  {
+    Toolkit::GetImplementation((*iter)->visual).DoAction( actionId, attributes );
+  }
+}
+
 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
 {
   Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
@@ -901,6 +970,26 @@ void Control::Impl::SetProperty( BaseObject* object, Property::Index index, cons
         break;
       }
 
+      case Toolkit::Control::Property::MARGIN:
+      {
+        Extents margin;
+        if( value.Get( margin ) )
+        {
+          controlImpl.mImpl->SetMargin( margin );
+        }
+        break;
+      }
+
+      case Toolkit::Control::Property::PADDING:
+      {
+        Extents padding;
+        if( value.Get( padding ) )
+        {
+          controlImpl.mImpl->SetPadding( padding );
+        }
+        break;
+      }
+
       case Toolkit::DevelControl::Property::TOOLTIP:
       {
         TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
@@ -909,7 +998,9 @@ void Control::Impl::SetProperty( BaseObject* object, Property::Index index, cons
           tooltipPtr = Tooltip::New( control );
         }
         tooltipPtr->SetProperties( value );
+        break;
       }
+
     }
   }
 }
@@ -1007,6 +1098,18 @@ Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index
         break;
       }
 
+      case Toolkit::Control::Property::MARGIN:
+      {
+        value = controlImpl.mImpl->GetMargin();
+        break;
+      }
+
+      case Toolkit::Control::Property::PADDING:
+      {
+        value = controlImpl.mImpl->GetPadding();
+        break;
+      }
+
       case Toolkit::DevelControl::Property::TOOLTIP:
       {
         Property::Map map;
@@ -1017,7 +1120,6 @@ Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index
         value = map;
         break;
       }
-
     }
   }
 
@@ -1086,8 +1188,8 @@ void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisu
       Property::Map fromMap;
       visual.CreatePropertyMap( fromMap );
 
-      Toolkit::DevelVisual::Type fromType = GetVisualTypeFromMap( fromMap );
-      Toolkit::DevelVisual::Type toType = GetVisualTypeFromMap( toMap );
+      Toolkit::Visual::Type fromType = GetVisualTypeFromMap( fromMap );
+      Toolkit::Visual::Type toType = GetVisualTypeFromMap( toMap );
 
       if( fromType != toType )
       {
@@ -1095,8 +1197,8 @@ void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisu
       }
       else
       {
-        if( fromType == Toolkit::DevelVisual::IMAGE || fromType == Toolkit::DevelVisual::N_PATCH
-            || fromType == Toolkit::DevelVisual::SVG || fromType == Toolkit::DevelVisual::ANIMATED_IMAGE )
+        if( fromType == Toolkit::Visual::IMAGE || fromType == Toolkit::Visual::N_PATCH
+            || fromType == Toolkit::Visual::SVG || fromType == Toolkit::Visual::ANIMATED_IMAGE )
         {
           Property::Value* fromUrl = fromMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
           Property::Value* toUrl = toMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
@@ -1263,6 +1365,116 @@ void Control::Impl::SetSubState( const std::string& subStateName, bool withTrans
   }
 }
 
+void Control::Impl::OnStageDisconnection()
+{
+  Actor self = mControlImpl.Self();
+
+  // Any visuals set for replacement but not yet ready should still be registered.
+  // Reason: If a request was made to register a new visual but the control removed from stage before visual was ready
+  // then when this control appears back on stage it should use that new visual.
+
+  // Iterate through all registered visuals and set off stage
+  SetVisualsOffStage( mVisuals, self );
+
+  // Visuals pending replacement can now be taken out of the removal list and set off stage
+  // Iterate through all replacement visuals and add to a move queue then set off stage
+  for( auto removalIter = mRemoveVisuals.Begin(), end = mRemoveVisuals.End(); removalIter != end; removalIter++ )
+  {
+    Toolkit::GetImplementation((*removalIter)->visual).SetOffStage( self );
+  }
+
+  for( auto replacedIter = mVisuals.Begin(), end = mVisuals.End(); replacedIter != end; replacedIter++ )
+  {
+    (*replacedIter)->pending = false;
+  }
+
+  mRemoveVisuals.Clear();
+}
+
+void Control::Impl::SetMargin( Extents margin )
+{
+  mControlImpl.mImpl->mMargin = margin;
+
+  // Trigger a size negotiation request that may be needed when setting a margin.
+  mControlImpl.RelayoutRequest();
+}
+
+Extents Control::Impl::GetMargin() const
+{
+  return mControlImpl.mImpl->mMargin;
+}
+
+void Control::Impl::SetPadding( Extents padding )
+{
+  mControlImpl.mImpl->mPadding = padding;
+
+  // Trigger a size negotiation request that may be needed when setting a padding.
+  mControlImpl.RelayoutRequest();
+}
+
+Extents Control::Impl::GetPadding() const
+{
+  return mControlImpl.mImpl->mPadding;
+}
+
+void Control::Impl::SetInputMethodContext( InputMethodContext& inputMethodContext )
+{
+  mInputMethodContext = inputMethodContext;
+}
+
+bool Control::Impl::FilterKeyEvent( const KeyEvent& event )
+{
+  bool consumed ( false );
+
+  if ( mInputMethodContext )
+  {
+    consumed = mInputMethodContext.FilterEventKey( event );
+  }
+  return consumed;
+}
+
+Toolkit::Internal::LayoutItemPtr Control::Impl::GetLayout() const
+{
+  return mLayout;
+}
+
+void Control::Impl::SetLayout( Toolkit::Internal::LayoutItem& layout )
+{
+  DALI_LOG_INFO( gLogFilterLayout, Debug::Verbose, "Control::SetLayout control:%s  existing layout:%s\n",
+                                   mControlImpl.Self().GetName().c_str(),
+                                   mLayout?"true":"false" );
+
+  if( mLayout )
+  {
+    mLayout->Unparent();
+    mLayout.Reset();
+  }
+  mLayout = &layout;
+
+  auto controlHandle = Toolkit::Control::DownCast( mControlImpl.Self() ); // Get a handle of this control implementation without copying internals.
+  mLayout->Initialize( controlHandle, controlHandle.GetTypeName() ); // LayoutGroup takes ownership of existing children
+}
+
+void Control::Impl::RemoveLayout()
+{
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::Impl::RemoveLayout\n");
+  if( mLayout )
+  {
+    mLayout->Unparent();
+    mLayout.Reset();
+  }
+}
+
+void Control::Impl::SetLayoutingRequired( bool layoutingRequired )
+{
+  mControlImpl.mImpl->mIsLayoutingRequired = layoutingRequired;
+}
+
+bool Control::Impl::IsLayoutingRequired()
+{
+  return mControlImpl.mImpl->mIsLayoutingRequired;
+}
+
 } // namespace Internal
 
 } // namespace Toolkit