Remove constraints from Popup, TableView, Alignment & ScrollBar
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-impl.cpp
index 226883e..82a502f 100644 (file)
@@ -1,27 +1,29 @@
-//
-// 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.
+ *
+ */
 
 // CLASS HEADER
 #include <dali-toolkit/internal/builder/builder-impl.h>
 
 // EXTERNAL INCLUDES
 #include <sys/stat.h>
+#include <boost/function.hpp>
+#include <sstream>
 
 // INTERNAL INCLUDES
-#include <dali/dali.h>
 #include <dali/integration-api/debug.h>
 
 #include <dali-toolkit/public-api/controls/control.h>
@@ -42,15 +44,14 @@ namespace Internal
 {
 class Replacement;
 
-extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot );
+extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements );
 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements );
-extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor);
-extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor);
-extern Actor SetupActor( const TreeNode& node, Actor& actor );
-extern Control SetupControl( const TreeNode& node, Control& actor );
+extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
+extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
+extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
 
 #if defined(DEBUG_ENABLED)
 Integration::Log::Filter* gFilterScript  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
@@ -85,6 +86,7 @@ std::string ToString(const Rect<int>& value)
   return ss.str();
 }
 
+#if defined(DEBUG_ENABLED)
 
 std::string PropertyValueToString( const Property::Value& value )
 {
@@ -164,7 +166,7 @@ std::string PropertyValueToString( const Property::Value& value )
     }
     case Property::MAP:
     {
-      ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().size() );
+      ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
       break;
     }
     case Property::TYPE_COUNT:
@@ -176,6 +178,7 @@ std::string PropertyValueToString( const Property::Value& value )
 
   return ret;
 }
+#endif // DEBUG_ENABLED
 
 /*
  * Recursively collects all stylesin a node (An array of style names).
@@ -207,6 +210,23 @@ void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style,
   }
 }
 
+struct QuitAction
+{
+public:
+  QuitAction( Builder& builder )
+  : mBuilder( builder )
+  {
+  }
+
+  void operator()(void)
+  {
+    mBuilder.EmitQuitSignal();
+  }
+
+private:
+  Builder& mBuilder;
+};
+
 } // namespace anon
 
 /*
@@ -222,7 +242,7 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace
 
       std::string key( keyChild.first );
 
-      // ignore special fields; type,actors,signals
+      // ignore special fields; type,actors,signals,styles
       if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
       {
         continue;
@@ -252,12 +272,15 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace
       // special field 'effect' references the shader effect instances
       if(key == "effect")
       {
-        Actor actor = Actor::DownCast(handle);
-        OptionalString s = constant.IsString( keyChild.second );
-        if(actor && s)
+        RenderableActor actor = RenderableActor::DownCast(handle);
+        if( actor )
         {
-          ShaderEffect e = GetShaderEffect(*s, constant);
-          actor.SetShaderEffect(e);
+          OptionalString str = constant.IsString( keyChild.second );
+          if( str )
+          {
+            ShaderEffect effect = GetShaderEffect( *str, constant );
+            actor.SetShaderEffect(effect);
+          }
         }
         else
         {
@@ -267,11 +290,29 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace
         continue;
       }
 
-      Property::Index index = handle.GetPropertyIndex(key);
+      Handle propertyObject( handle );
+
+      Property::Index index = propertyObject.GetPropertyIndex( key );
+
+      if( Property::INVALID_INDEX == index )
+      {
+        RenderableActor actor = RenderableActor::DownCast(handle);
+        if( actor )
+        {
+          if( ShaderEffect effect = actor.GetShaderEffect() )
+          {
+            index = effect.GetPropertyIndex( key );
+            if(index != Property::INVALID_INDEX)
+            {
+              propertyObject = effect;
+            }
+          }
+        }
+      }
 
       if( Property::INVALID_INDEX != index )
       {
-        Property::Type type = handle.GetPropertyType(index);
+        Property::Type type = propertyObject.GetPropertyType(index);
 
         Property::Value value;
         if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
@@ -284,7 +325,7 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace
         {
           DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
 
-          handle.SetProperty( index, value );
+          propertyObject.SetProperty( index, value );
         }
       }
       else
@@ -310,19 +351,12 @@ void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
 
     if( actor )
     {
-      SetupActor( node, actor );
-
-      Control control  = Control::DownCast(actor);
-
-      if( control )
-      {
-        SetupControl( node, control );
-      }
+      SetupActor( node, actor, constant );
 
       // add signals
-      SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor );
-
-      SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor );
+      QuitAction quitAction( *this );
+      SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
+      SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
    }
   }
   else
@@ -454,7 +488,6 @@ BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
     baseHandle       = typeInfo.CreateInstance();
     Handle handle    = Handle::DownCast(baseHandle);
     Actor actor      = Actor::DownCast(handle);
-    Control control  = Control::DownCast(handle);
 
     if(handle)
     {
@@ -473,6 +506,7 @@ BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
         DALI_SCRIPT_VERBOSE("  Is Actor id=%d\n", actor.GetId());
       }
 
+      Toolkit::Control control  = Toolkit::Control::DownCast(handle);
       if(control)
       {
         DALI_SCRIPT_VERBOSE("  Is Control id=%d\n", actor.GetId());
@@ -492,9 +526,7 @@ BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
         }
       }
 
-      ApplyProperties( root, node, handle, replacements );
-
-      if( actor)
+      if( actor )
       {
         // add children of all the styles
         if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
@@ -514,7 +546,10 @@ BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
           parent.Add( actor );
         }
       }
-
+      else
+      {
+        ApplyProperties( root, node, handle, replacements );
+      }
     }
     else
     {
@@ -525,19 +560,6 @@ BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
   return baseHandle;
 }
 
-
-ActorContainer Builder::GetTopLevelActors() const
-{
-  // deprecated function.
-  return ActorContainer();
-}
-
-Animation Builder::GetAnimation( const std::string &name ) const
-{
-  // deprecated
-  return Animation();
-}
-
 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
 {
   const Stage& stage = Stage::GetCurrent();
@@ -713,7 +735,7 @@ FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Re
         if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
         {
           propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
-          ret = Dali::Scripting::NewImage( propertyMap );
+          ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
           mFrameBufferImageLut[ name ] = ret;
         }
       }
@@ -723,29 +745,77 @@ FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Re
   return ret;
 }
 
-Font Builder::GetFont( const std::string& name ) const
+Path Builder::GetPath( const std::string& name )
 {
-  // deprecated function.
-  Font font;
-  return font;
-}
+  DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
 
-TextStyle Builder::GetTextStyle( const std::string& name ) const
-{
-  // deprecated
-  return TextStyle();
+  Path ret;
+
+  PathLut::const_iterator iter( mPathLut.find( name ) );
+  if( iter != mPathLut.end() )
+  {
+    ret = iter->second;
+  }
+  else
+  {
+    if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
+    {
+      if( OptionalChild path = IsChild( *paths, name ) )
+      {
+        //points property
+        if( OptionalChild pointsProperty = IsChild( *path, "points") )
+        {
+          Dali::Property::Value points(Property::ARRAY);
+          if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
+          {
+            ret = Path::New();
+            ret.SetProperty( Path::POINTS, points);
+
+            //control-points property
+            if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
+            {
+              Dali::Property::Value points(Property::ARRAY);
+              if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
+              {
+                ret.SetProperty( Path::CONTROL_POINTS, points);
+              }
+            }
+            else
+            {
+              //Curvature
+              float curvature(0.25f);
+              if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
+              {
+                curvature = *pointsProperty;
+              }
+              ret.GenerateControlPoints(curvature);
+            }
+
+            //Add the new path to the hash table for paths
+            mPathLut[ name ] = ret;
+          }
+        }
+        else
+        {
+          //Interpolation points not specified
+          DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
+        }
+      }
+
+    }
+  }
+
+  return ret;
 }
 
-Image Builder::GetImage( const std::string& name) const
+Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
 {
-  // deprecated function.
-  return Image();
+  return mQuitSignal;
 }
 
-Actor Builder::GetActor( const std::string &name ) const
+void Builder::EmitQuitSignal()
 {
-  // deprecated function.
-  return Actor();
+  mQuitSignal.Emit();
 }
 
 void Builder::AddActors( Actor toActor )
@@ -758,7 +828,7 @@ void Builder::AddActors( const std::string &sectionName, Actor toActor )
 {
   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
 
-  PropertyValueMap overrideMap;
+  Property::Map overrideMap;
   Replacement replacements(overrideMap, mReplacementMap);
 
   OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
@@ -801,7 +871,7 @@ Animation Builder::CreateAnimation( const std::string& animationName, const Repl
   {
     if( OptionalChild animation = IsChild(*animations, animationName) )
     {
-      anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor );
+      anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
     }
     else
     {
@@ -816,13 +886,13 @@ Animation Builder::CreateAnimation( const std::string& animationName, const Repl
   return anim;
 }
 
-Animation Builder::CreateAnimation( const std::string& animationName, const PropertyValueMap& map, Dali::Actor sourceActor )
+Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
 {
   Replacement replacement(map, mReplacementMap);
   return CreateAnimation( animationName, replacement, sourceActor);
 }
 
-Animation Builder::CreateAnimation( const std::string& animationName, const PropertyValueMap& map )
+Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
 {
   Replacement replacement(map, mReplacementMap);
   return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
@@ -844,8 +914,6 @@ Animation Builder::CreateAnimation( const std::string& animationName )
 
 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
 {
-  DALI_ASSERT_ALWAYS( format == Dali::Toolkit::Builder::JSON && "Currently only JSON is supported" );
-
   // parser to get constants and includes only
   Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
 
@@ -898,12 +966,9 @@ void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::U
 
 }
 
-void Builder::AddConstants( const PropertyValueMap& map )
+void Builder::AddConstants( const Property::Map& map )
 {
-  for(PropertyValueMap::const_iterator iter = map.begin(); iter != map.end(); ++iter)
-  {
-    mReplacementMap[ (*iter).first ] = (*iter).second;
-  }
+  mReplacementMap.Merge( map );
 }
 
 void Builder::AddConstant( const std::string& key, const Property::Value& value )
@@ -911,17 +976,17 @@ void Builder::AddConstant( const std::string& key, const Property::Value& value
   mReplacementMap[key] = value;
 }
 
-const PropertyValueMap& Builder::GetConstants() const
+const Property::Map& Builder::GetConstants() const
 {
   return mReplacementMap;
 }
 
 const Property::Value& Builder::GetConstant( const std::string& key ) const
 {
-  PropertyValueMap::const_iterator iter = mReplacementMap.find( key );
-  if( iter  != mReplacementMap.end() )
+  Property::Value* match = mReplacementMap.Find( key );
+  if( match )
   {
-    return (*iter).second;
+    return (*match);
   }
   else
   {
@@ -930,7 +995,7 @@ const Property::Value& Builder::GetConstant( const std::string& key ) const
   }
 }
 
-void Builder::LoadConstants( const TreeNode& root, PropertyValueMap& intoMap )
+void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
 {
   Replacement replacer(intoMap);
 
@@ -959,10 +1024,10 @@ void Builder::LoadConstants( const TreeNode& root, PropertyValueMap& intoMap )
   }
 
 #if defined(DEBUG_ENABLED)
-  PropertyValueMap::const_iterator iter = intoMap.find( "CONFIG_SCRIPT_LOG_LEVEL" );
-  if( iter != intoMap.end() && (*iter).second.GetType() == Property::STRING )
+  Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
+  if( iter && iter->GetType() == Property::STRING )
   {
-    std::string logLevel( (*iter).second.Get< std::string >() );
+    std::string logLevel( iter->Get< std::string >() );
     if( logLevel == "NoLogging" )
     {
       gFilterScript->SetLogLevel( Integration::Log::NoLogging );
@@ -984,13 +1049,13 @@ void Builder::LoadConstants( const TreeNode& root, PropertyValueMap& intoMap )
 
 }
 
-void Builder::ApplyStyle( const std::string& styleName, Handle& handle )
+bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
 {
   Replacement replacer( mReplacementMap );
-  ApplyStyle( styleName, handle, replacer );
+  return ApplyStyle( styleName, handle, replacer );
 }
 
-void Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
+bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
 {
   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
 
@@ -1000,15 +1065,15 @@ void Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Re
   if( styles && style )
   {
     ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
+    return true;
   }
   else
   {
-    DALI_SCRIPT_WARNING("No styles section to create style '%s'\n", styleName.c_str());
+    return false;
   }
-
 }
 
-BaseHandle Builder::Create( const std::string& templateName, const PropertyValueMap& map )
+BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
 {
   Replacement replacement( map, mReplacementMap );
   return Create( templateName, replacement );
@@ -1051,6 +1116,44 @@ BaseHandle Builder::Create( const std::string& templateName, const Replacement&
   return baseHandle;
 }
 
+BaseHandle Builder::CreateFromJson( const std::string& json )
+{
+  BaseHandle ret;
+
+  // merge in new template, hoping no one else has one named '@temp@'
+  std::string newTemplate =
+    std::string("{\"templates\":{\"@temp@\":") +                      \
+    json +                                                            \
+    std::string("}}");
+
+  if( mParser.Parse(newTemplate) )
+  {
+    Replacement replacement( mReplacementMap );
+    ret = Create( "@temp@", replacement );
+  }
+
+  return ret;
+}
+
+bool Builder::ApplyFromJson(  Handle& handle, const std::string& json )
+{
+  bool ret = false;
+
+  // merge new style, hoping no one else has one named '@temp@'
+  std::string newStyle =
+    std::string("{\"styles\":{\"@temp@\":") +                           \
+    json +                                                              \
+    std::string("}}");
+
+  if( mParser.Parse(newStyle) )
+  {
+    Replacement replacement( mReplacementMap );
+    ret = ApplyStyle( "@temp@", handle, replacement );
+  }
+
+  return ret;
+}
+
 
 BaseHandle Builder::Create( const std::string& templateName )
 {