Remove deprecated SetRemoveTime API
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / relayout-controller-impl.cpp
index bdefe39..e235d98 100644 (file)
@@ -1,34 +1,32 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2014 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // FILE HEADER
 
 #include "relayout-controller-impl.h"
 
 // EXTERNAL INCLUDES
-
-#include <stack>
-#include <sstream>
 #include <dali/integration-api/debug.h>
+#if defined(DEBUG_ENABLED)
+#include <sstream>
+#endif // defined(DEBUG_ENABLED)
 
 // INTERNAL INCLUDES
-
-#include "dali-toolkit/public-api/controls/control.h"
-#include "dali-toolkit/public-api/controls/control-impl.h"
-#include "dali-toolkit/public-api/controls/text-view/text-view.h"
+#include <dali-toolkit/public-api/controls/text-view/text-view.h>
 
 namespace Dali
 {
@@ -36,9 +34,6 @@ namespace Dali
 namespace Toolkit
 {
 
-typedef std::pair< Control, Vector2 > ControlSizePair;
-typedef std::stack< ControlSizePair > ControlStack;
-
 namespace Internal
 {
 
@@ -85,7 +80,10 @@ void PrintChildren( Actor actor, int level )
  */
 void PrintHierarchy()
 {
-  PrintChildren( Stage().GetCurrent().GetRootLayer(), 0 );
+  if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
+  {
+    PrintChildren( Stage().GetCurrent().GetRootLayer(), 0 );
+  }
 }
 
 #define PRINT_HIERARCHY PrintHierarchy()
@@ -126,7 +124,7 @@ void SetIfNotZero( Vector2& target, const Vector2& source )
  */
 void FindControls( Actor actor, ControlStack& controls, Vector2 size )
 {
-  Control control( Control::DownCast( actor ) );
+  Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
   if( control )
   {
     // If the control size has been set by the application / control, then we should try and honour that.
@@ -135,7 +133,7 @@ void FindControls( Actor actor, ControlStack& controls, Vector2 size )
     // Only set the width and height if they are non zero.
     SetIfNotZero( size, controlSetSize );
 
-    controls.push( ControlSizePair( control, size ) );
+    controls.push_back( ControlSizePair( control, size ) );
   }
   else
   {
@@ -163,6 +161,15 @@ void PushToStack( ControlStack& controlStack, const ActorSizeContainer& containe
 
 } // unnamed namespace
 
+RelayoutControllerImpl::RelayoutControllerImpl( bool& relayoutFlag )
+: mRelayoutFlag( relayoutFlag ),
+  mRelayoutConnection( false )
+{
+  // make space for 32 controls to avoid having to copy construct a lot in the beginning
+  mControlStack.reserve( 32 );
+  mSizecontainer.reserve( 32 );
+}
+
 RelayoutControllerImpl::~RelayoutControllerImpl()
 {
 }
@@ -181,49 +188,40 @@ void RelayoutControllerImpl::Request()
 
 void RelayoutControllerImpl::Relayout()
 {
-  PRINT_HIERARCHY;
-
-  // 1. Finds all top-level controls from the root actor and allocate them the size of the stage
-  //    These controls are paired with the stage size and added to the stack.
-  ControlStack controlStack;
-  FindControls( Stage().GetCurrent().GetRootLayer(), controlStack, Stage::GetCurrent().GetSize() );
-
-  // 2. Iterate through the stack until it's empty.
-  while ( !controlStack.empty() )
+  // only do something when requested
+  if( mRelayoutFlag )
   {
-    ControlSizePair pair ( controlStack.top() );
-    Control control ( pair.first );
-    Vector2 size ( pair.second );
-    controlStack.pop();
+    // clear the flag as we're now doing the relayout
+    mRelayoutFlag = false;
+    PRINT_HIERARCHY;
 
-    DALI_LOG_INFO( gLogFilter, Debug::General, "Allocating %p (%.2f, %.2f)\n", control.GetObjectPtr(), size.width, size.height );
+    mControlStack.clear(); // we do not release memory, just empty the container
 
-    // 3. Negotiate the size with the current control. Pass it an empty container which the control
-    //    has to fill with all the actors it has not done any size negotiation for.
-    ActorSizeContainer container;
-    control.GetImplementation().NegotiateSize( size, container );
+    // 1. Finds all top-level controls from the root actor and allocate them the size of the stage
+    //    These controls are paired with the stage size and added to the stack.
+    FindControls( Stage().GetCurrent().GetRootLayer(), mControlStack, Stage::GetCurrent().GetSize() );
 
-    // 4. Push the controls from the actors in the container to the stack.
-    PushToStack( controlStack, container );
-  }
+    // 2. Iterate through the stack until it's empty.
+    while ( !mControlStack.empty() )
+    {
+      ControlSizePair pair ( mControlStack.back() );
+      Toolkit::Control control ( pair.first );
+      Vector2 size ( pair.second );
+      mControlStack.pop_back();
 
-  //Disconnect so that we relayout only when requested to do so.
-  Disconnect();
-}
+      DALI_LOG_INFO( gLogFilter, Debug::General, "Allocating %p (%.2f, %.2f)\n", control.GetObjectPtr(), size.width, size.height );
 
-void RelayoutControllerImpl::Disconnect()
-{
-  if( mRelayoutConnection )
-  {
-    Stage stage = Stage::GetCurrent();
-    stage.EventProcessingFinishedSignal().Disconnect( this, &RelayoutControllerImpl::Relayout );
-    mRelayoutConnection = false;
-  }
-}
+      mSizecontainer.clear();
+      // 3. Negotiate the size with the current control. Pass it an empty container which the control
+      //    has to fill with all the actors it has not done any size negotiation for.
+      control.GetImplementation().NegotiateSize( size, mSizecontainer );
 
-RelayoutControllerImpl::RelayoutControllerImpl()
-: mRelayoutConnection( false )
-{
+      // 4. Push the controls from the actors in the container to the stack.
+      PushToStack( mControlStack, mSizecontainer );
+    }
+  }
+  // should not disconnect the signal as that causes some control size negotiations to not work correctly
+  // this algorithm needs more optimization as well
 }
 
 } // namespace Internal