[dali_1.3.19] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/builder/builder-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <sys/stat.h>
23 #include <sstream>
24
25 #include <dali/public-api/actors/camera-actor.h>
26 #include <dali/public-api/actors/layer.h>
27 #include <dali/public-api/object/property-array.h>
28 #include <dali/public-api/object/type-info.h>
29 #include <dali/public-api/object/type-registry.h>
30 #include <dali/public-api/render-tasks/render-task-list.h>
31 #include <dali/public-api/signals/functor-delegate.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/integration-api/debug.h>
34 #include <dali-toolkit/devel-api/controls/control-devel.h>
35
36 // INTERNAL INCLUDES
37 #include <dali-toolkit/public-api/controls/control.h>
38 #include <dali-toolkit/devel-api/builder/json-parser.h>
39
40 #include <dali-toolkit/internal/builder/builder-declarations.h>
41 #include <dali-toolkit/internal/builder/builder-filesystem.h>
42 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
43 #include <dali-toolkit/internal/builder/builder-impl-debug.h>
44 #include <dali-toolkit/internal/builder/builder-set-property.h>
45 #include <dali-toolkit/internal/builder/replacement.h>
46 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
47
48 namespace Dali
49 {
50
51 namespace Toolkit
52 {
53
54 namespace Internal
55 {
56 class Replacement;
57
58 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
59
60 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
61
62 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
63
64
65 #if defined(DEBUG_ENABLED)
66 Integration::Log::Filter* gFilterScript  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
67 #endif
68
69 namespace
70 {
71
72 #define TOKEN_STRING(x) #x
73
74 const std::string KEYNAME_ACTORS           = "actors";
75 const std::string KEYNAME_ENTRY_TRANSITION = "entryTransition";
76 const std::string KEYNAME_EXIT_TRANSITION  = "exitTransition";
77 const std::string KEYNAME_INCLUDES         = "includes";
78 const std::string KEYNAME_INHERIT          = "inherit";
79 const std::string KEYNAME_MAPPINGS         = "mappings";
80 const std::string KEYNAME_NAME             = "name";
81 const std::string KEYNAME_SIGNALS          = "signals";
82 const std::string KEYNAME_STATES           = "states";
83 const std::string KEYNAME_STYLES           = "styles";
84 const std::string KEYNAME_TEMPLATES        = "templates";
85 const std::string KEYNAME_TRANSITIONS      = "transitions";
86 const std::string KEYNAME_TYPE             = "type";
87 const std::string KEYNAME_VISUALS          = "visuals";
88
89 const std::string PROPERTIES = "properties";
90 const std::string ANIMATABLE_PROPERTIES = "animatableProperties";
91
92 typedef std::vector<const TreeNode*> TreeNodeList;
93
94
95 bool GetMappingKey( const std::string& str, std::string& key )
96 {
97   bool result = false;
98   std::string test( str );
99   if( ! test.empty() )
100   {
101     if( test.at(0) == '<' )
102     {
103       if( test.at(test.length()-1) == '>' )
104       {
105         key = test.substr( 1, test.length()-2 );
106         result = true;
107       }
108     }
109   }
110   return result;
111 }
112
113 /*
114  * Recursively collects all styles in a node (An array of style names).
115  *
116  * stylesCollection The set of styles from the json file (a json object of named styles)
117  * style The style array to begin the collection from
118  * styleList The style list to add nodes to apply
119  */
120 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
121 {
122   // style is an array of style names
123   if( TreeNode::ARRAY == style.GetType() )
124   {
125     for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
126     {
127       if( OptionalString styleName = IsString( (*iter).second ) )
128       {
129         if( OptionalChild node = IsChildIgnoreCase( stylesCollection, *styleName) )
130         {
131           styleList.push_back( &(*node) );
132
133           OptionalChild subStyle = IsChild( *node, KEYNAME_INHERIT );
134           if( ! subStyle )
135           {
136             subStyle = IsChild( *node, KEYNAME_STYLES );
137           }
138           if( subStyle )
139           {
140             CollectAllStyles( stylesCollection, *subStyle, styleList );
141           }
142         }
143       }
144     }
145   }
146 }
147
148
149 } // namespace anon
150
151
152 Builder::Builder()
153 : mSlotDelegate( this )
154 {
155   mParser = Dali::Toolkit::JsonParser::New();
156
157   Property::Map defaultDirs;
158   defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ]       = DALI_IMAGE_DIR;
159   defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ]       = DALI_SOUND_DIR;
160   defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ]       = DALI_STYLE_DIR;
161   defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR;
162
163   AddConstants( defaultDirs );
164 }
165
166 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
167 {
168   // parser to get constants and includes only
169   Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
170
171   if( !parser.Parse( data ) )
172   {
173     DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
174                       parser.GetErrorLineNumber(),
175                       parser.GetErrorColumn(),
176                       parser.GetErrorDescription().c_str() );
177
178     DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
179   }
180   else
181   {
182     // load configuration map
183     LoadConfiguration( *parser.GetRoot(), mConfigurationMap );
184     // load constant map (allows the user to override the constants in the json after loading)
185     LoadConstants( *parser.GetRoot(), mReplacementMap );
186     // merge includes
187     if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
188     {
189       Replacement replacer( mReplacementMap );
190
191       for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
192       {
193         OptionalString filename = replacer.IsString( (*iter).second );
194
195         if( filename )
196         {
197 #if defined(DEBUG_ENABLED)
198           DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
199 #endif
200           LoadFromString( GetFileContents(*filename) );
201         }
202       }
203     }
204
205     if( mParser.Parse( data ) )
206     {
207       // Drop the styles and get them to be rebuilt against the new parse tree as required.
208       mStyles.Clear();
209     }
210     else
211     {
212       DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
213                         mParser.GetErrorLineNumber(),
214                         mParser.GetErrorColumn(),
215                         mParser.GetErrorDescription().c_str() );
216
217       DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
218     }
219   }
220
221   DUMP_PARSE_TREE(mParser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
222   DUMP_TEST_MAPPINGS(mParser);
223
224   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
225 }
226
227 void Builder::AddConstants( const Property::Map& map )
228 {
229   mReplacementMap.Merge( map );
230 }
231
232 void Builder::AddConstant( const std::string& key, const Property::Value& value )
233 {
234   mReplacementMap[key] = value;
235 }
236
237 const Property::Map& Builder::GetConfigurations() const
238 {
239   return mConfigurationMap;
240 }
241
242 const Property::Map& Builder::GetConstants() const
243 {
244   return mReplacementMap;
245 }
246
247 const Property::Value& Builder::GetConstant( const std::string& key ) const
248 {
249   Property::Value* match = mReplacementMap.Find( key );
250   if( match )
251   {
252     return (*match);
253   }
254   else
255   {
256     static Property::Value invalid;
257     return invalid;
258   }
259 }
260
261 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
262 {
263   Replacement replacement(map, mReplacementMap);
264   return CreateAnimation( animationName, replacement, sourceActor);
265 }
266
267 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
268 {
269   Replacement replacement(map, mReplacementMap);
270   return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
271 }
272
273 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
274 {
275   Replacement replacement( mReplacementMap );
276
277   return CreateAnimation( animationName, replacement, sourceActor );
278 }
279
280 Animation Builder::CreateAnimation( const std::string& animationName )
281 {
282   Replacement replacement( mReplacementMap );
283
284   return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
285 }
286
287 BaseHandle Builder::Create( const std::string& templateName )
288 {
289   Replacement replacement( mReplacementMap );
290   return Create( templateName, replacement );
291 }
292
293 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
294 {
295   Replacement replacement( map, mReplacementMap );
296   return Create( templateName, replacement );
297 }
298
299 BaseHandle Builder::CreateFromJson( const std::string& json )
300 {
301   BaseHandle ret;
302
303   // merge in new template, hoping no one else has one named '@temp@'
304   std::string newTemplate =
305     std::string("{\"templates\":{\"@temp@\":") +                      \
306     json +                                                            \
307     std::string("}}");
308
309   if( mParser.Parse(newTemplate) )
310   {
311     Replacement replacement( mReplacementMap );
312     ret = Create( "@temp@", replacement );
313   }
314
315   return ret;
316 }
317
318 bool Builder::ApplyFromJson(  Handle& handle, const std::string& json )
319 {
320   bool ret = false;
321
322   // merge new style, hoping no one else has one named '@temp@'
323   std::string newStyle =
324     std::string("{\"styles\":{\"@temp@\":") +                           \
325     json +                                                              \
326     std::string("}}");
327
328   if( mParser.Parse(newStyle) )
329   {
330     Replacement replacement( mReplacementMap );
331     ret = ApplyStyle( "@temp@", handle, replacement );
332   }
333
334   return ret;
335 }
336
337 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
338 {
339   Replacement replacer( mReplacementMap );
340   return ApplyStyle( styleName, handle, replacer );
341 }
342
343 bool Builder::LookupStyleName( const std::string& styleName )
344 {
345   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
346
347   OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
348   OptionalChild style  = IsChildIgnoreCase( *styles, styleName );
349
350   if( styles && style )
351   {
352     return true;
353   }
354   return false;
355 }
356
357 const StylePtr Builder::GetStyle( const std::string& styleName )
358 {
359   const StylePtr* style = mStyles.FindConst( styleName );
360
361   if( style==NULL )
362   {
363     return StylePtr(NULL);
364   }
365   else
366   {
367     return *style;
368   }
369 }
370
371 void Builder::AddActors( Actor toActor )
372 {
373   // 'stage' is the default/by convention section to add from
374   AddActors( "stage", toActor );
375 }
376
377 void Builder::AddActors( const std::string &sectionName, Actor toActor )
378 {
379   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
380
381   Property::Map overrideMap;
382   Replacement replacements(overrideMap, mReplacementMap);
383
384   OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
385
386   if( add )
387   {
388     for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
389     {
390       // empty actor adds directly to the stage
391       BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
392       Actor actor = Actor::DownCast(baseHandle);
393       if(actor)
394       {
395         toActor.Add( actor );
396       }
397     }
398
399     // if were adding the 'stage' section then also check for a render task called stage
400     // to add automatically
401     if( "stage" == sectionName )
402     {
403       if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") )
404       {
405         if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
406         {
407           CreateRenderTask( "stage" );
408         }
409       }
410     }
411   }
412 }
413
414 void Builder::CreateRenderTask( const std::string &name )
415 {
416   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
417
418   Replacement constant(mReplacementMap);
419
420   const Stage& stage = Stage::GetCurrent();
421
422   OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks");
423
424   if(tasks)
425   {
426     //
427     // Create the tasks from the current task as generally we want
428     // to setup task zero and onwards. Although this does overwrite
429     // the properties of the current task.
430     //
431     if( OptionalChild renderTask = IsChild(*tasks, name ) )
432     {
433       RenderTaskList list = stage.GetRenderTaskList();
434       unsigned int start = list.GetTaskCount();
435
436       RenderTask task;
437       if(0 == start)
438       {
439         // zero should have already been created by the stage so really
440         // this case should never happen
441         task = list.CreateTask();
442         start++;
443       }
444
445       TreeNode::ConstIterator iter = (*renderTask).CBegin();
446       task = list.GetTask( start - 1 );
447
448       SetupTask( task, (*iter).second, constant  );
449
450       ++iter;
451
452       for(; iter != (*renderTask).CEnd(); ++iter )
453       {
454         task = list.CreateTask();
455         SetupTask( task, (*iter).second, constant );
456       }
457     }
458   }
459 }
460
461 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
462 {
463   Replacement constant( mReplacementMap );
464   return GetFrameBufferImage(name, constant);
465 }
466
467 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
468 {
469   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
470
471   FrameBufferImage ret;
472
473   ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
474   if( iter != mFrameBufferImageLut.end() )
475   {
476     ret = iter->second;
477   }
478   else
479   {
480     if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") )
481     {
482       if( OptionalChild image = IsChild( *images, name ) )
483       {
484         Dali::Property::Value property(Property::MAP);
485         if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) )
486         {
487           Property::Map* map = property.GetMap();
488
489           if( map )
490           {
491             (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
492             ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
493             mFrameBufferImageLut[ name ] = ret;
494           }
495         }
496       }
497     }
498   }
499
500   return ret;
501 }
502
503 Path Builder::GetPath( const std::string& name )
504 {
505   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
506
507   Path ret;
508
509   PathLut::const_iterator iter( mPathLut.find( name ) );
510   if( iter != mPathLut.end() )
511   {
512     ret = iter->second;
513   }
514   else
515   {
516     if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
517     {
518       if( OptionalChild path = IsChild( *paths, name ) )
519       {
520         //points property
521         if( OptionalChild pointsProperty = IsChild( *path, "points") )
522         {
523           Dali::Property::Value points(Property::ARRAY);
524           if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
525           {
526             ret = Path::New();
527             ret.SetProperty( Path::Property::POINTS, points);
528
529             //controlPoints property
530             if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") )
531             {
532               Dali::Property::Value points(Property::ARRAY);
533               if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
534               {
535                 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
536               }
537             }
538             else
539             {
540               //Curvature
541               float curvature(0.25f);
542               if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
543               {
544                 curvature = *pointsProperty;
545               }
546               ret.GenerateControlPoints(curvature);
547             }
548
549             //Add the new path to the hash table for paths
550             mPathLut[ name ] = ret;
551           }
552         }
553         else
554         {
555           //Interpolation points not specified
556           DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
557         }
558       }
559
560     }
561   }
562
563   return ret;
564 }
565
566 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
567 {
568   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
569
570   //Search the pathConstrainer in the LUT
571   size_t count( mPathConstrainerLut.size() );
572   for( size_t i(0); i!=count; ++i )
573   {
574     if( mPathConstrainerLut[i].name == name )
575     {
576       //PathConstrainer has already been created
577       return mPathConstrainerLut[i].pathConstrainer;
578     }
579   }
580
581   //Create a new PathConstrainer
582   PathConstrainer ret;
583   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
584   {
585     if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
586     {
587       OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
588       if(!constrainerType)
589       {
590         DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
591       }
592       else if( *constrainerType == "PathConstrainer")
593       {
594         //points property
595         if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
596         {
597           Dali::Property::Value points(Property::ARRAY);
598           if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
599           {
600             ret = PathConstrainer::New();
601             ret.SetProperty( PathConstrainer::Property::POINTS, points);
602
603             //controlPoints property
604             if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") )
605             {
606               Dali::Property::Value points(Property::ARRAY);
607               if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
608               {
609                 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
610               }
611
612               //Forward vector
613               OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
614               if( forward )
615               {
616                 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
617               }
618
619               //Add the new constrainer to the vector of PathConstrainer
620               PathConstrainerEntry entry = {name,ret};
621               mPathConstrainerLut.push_back( entry );
622             }
623             else
624             {
625               //Control points not specified
626               DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
627             }
628           }
629         }
630         else
631         {
632           //Interpolation points not specified
633           DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
634         }
635       }
636       else
637       {
638         DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
639       }
640     }
641   }
642
643   return ret;
644 }
645
646
647 bool Builder::IsPathConstrainer( const std::string& name )
648 {
649   size_t count( mPathConstrainerLut.size() );
650   for( size_t i(0); i!=count; ++i )
651   {
652     if( mPathConstrainerLut[i].name == name )
653     {
654       return true;
655     }
656   }
657
658   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
659   {
660     if( OptionalChild constrainer = IsChild( *constrainers, name ) )
661     {
662       OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
663       if(!constrainerType)
664       {
665         return false;
666       }
667       else
668       {
669          return *constrainerType == "PathConstrainer";
670       }
671     }
672   }
673   return false;
674 }
675
676 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
677 {
678   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
679
680   //Search the LinearConstrainer in the LUT
681   size_t count( mLinearConstrainerLut.size() );
682   for( size_t i(0); i!=count; ++i )
683   {
684     if( mLinearConstrainerLut[i].name == name )
685     {
686       //LinearConstrainer has already been created
687       return mLinearConstrainerLut[i].linearConstrainer;
688     }
689   }
690
691   //Create a new LinearConstrainer
692   LinearConstrainer ret;
693   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
694   {
695     if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
696     {
697       OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
698       if(!constrainerType)
699       {
700         DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
701       }
702       else if( *constrainerType == "LinearConstrainer")
703       {
704         //points property
705         if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
706         {
707           Dali::Property::Value points(Property::ARRAY);
708           if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
709           {
710             ret = Dali::LinearConstrainer::New();
711             ret.SetProperty( LinearConstrainer::Property::VALUE, points);
712
713             //controlPoints property
714             if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
715             {
716               Dali::Property::Value points(Property::ARRAY);
717               if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
718               {
719                 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
720               }
721             }
722             //Add the new constrainer to vector of LinearConstrainer
723             LinearConstrainerEntry entry = {name,ret};
724             mLinearConstrainerLut.push_back( entry );
725           }
726         }
727         else
728         {
729           //Interpolation points not specified
730           DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
731         }
732       }
733       else
734       {
735         DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
736       }
737     }
738   }
739
740   return ret;
741 }
742
743 bool Builder::IsLinearConstrainer( const std::string& name )
744 {
745   // Search the LinearConstrainer in the LUT
746   size_t count( mLinearConstrainerLut.size() );
747   for( size_t i(0); i!=count; ++i )
748   {
749     if( mLinearConstrainerLut[i].name == name )
750     {
751       return true;
752     }
753   }
754
755   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
756   {
757     if( OptionalChild constrainer = IsChild( *constrainers, name ) )
758     {
759       OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
760       if(!constrainerType)
761       {
762         return false;
763       }
764       else
765       {
766          return *constrainerType == "LinearConstrainer";
767       }
768     }
769   }
770   return false;
771 }
772
773 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
774 {
775   return mQuitSignal;
776 }
777
778 void Builder::EmitQuitSignal()
779 {
780   mQuitSignal.Emit();
781 }
782
783 Builder::~Builder()
784 {
785 }
786
787 void Builder::LoadConfiguration( const TreeNode& root, Property::Map& intoMap )
788 {
789   Replacement replacer(intoMap);
790
791   if( OptionalChild constants = IsChild(root, "config") )
792   {
793     for(TreeNode::ConstIterator iter = (*constants).CBegin();
794         iter != (*constants).CEnd(); ++iter)
795     {
796       Dali::Property::Value property;
797       if( (*iter).second.GetName() )
798       {
799         DeterminePropertyFromNode( (*iter).second, property, replacer );
800         intoMap[ (*iter).second.GetName() ] = property;
801       }
802     }
803   }
804 }
805
806 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
807 {
808   Replacement replacer(intoMap);
809
810   if( OptionalChild constants = IsChild(root, "constants") )
811   {
812     for(TreeNode::ConstIterator iter = (*constants).CBegin();
813         iter != (*constants).CEnd(); ++iter)
814     {
815       Dali::Property::Value property;
816       if( (*iter).second.GetName() )
817       {
818 #if defined(DEBUG_ENABLED)
819         DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
820 #endif
821         DeterminePropertyFromNode( (*iter).second, property, replacer );
822         intoMap[ (*iter).second.GetName() ] = property;
823       }
824     }
825   }
826
827 #if defined(DEBUG_ENABLED)
828   Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
829   if( iter && iter->GetType() == Property::STRING )
830   {
831     std::string logLevel( iter->Get< std::string >() );
832     if( logLevel == "NoLogging" )
833     {
834       gFilterScript->SetLogLevel( Integration::Log::NoLogging );
835     }
836     else if( logLevel == "Concise" )
837     {
838       gFilterScript->SetLogLevel( Integration::Log::Concise );
839     }
840     else if( logLevel == "General" )
841     {
842       gFilterScript->SetLogLevel( Integration::Log::General );
843     }
844     else if( logLevel == "Verbose" )
845     {
846       gFilterScript->SetLogLevel( Integration::Log::Verbose );
847     }
848   }
849 #endif
850 }
851
852 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
853 {
854   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
855
856   Animation anim;
857
858   if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
859   {
860     if( OptionalChild animation = IsChild(*animations, animationName) )
861     {
862       anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
863     }
864     else
865     {
866       DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
867     }
868   }
869   else
870   {
871     DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
872   }
873
874   return anim;
875 }
876
877 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
878 {
879   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
880
881   BaseHandle baseHandle;
882
883   OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
884
885   if( !templates )
886   {
887     DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
888   }
889   else
890   {
891     OptionalChild childTemplate = IsChild(*templates, templateName);
892     if(!childTemplate)
893     {
894       DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
895     }
896     else
897     {
898       OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
899
900       if(!type)
901       {
902         DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
903       }
904       else
905       {
906         baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
907       }
908     }
909   }
910
911   return baseHandle;
912 }
913
914 /*
915  * Create a dali type from a node.
916  * If parent given and an actor type was created then add it to the parent and
917  * recursively add nodes children.
918  */
919 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
920                               Actor parent, const Replacement& replacements )
921 {
922   BaseHandle baseHandle;
923   TypeInfo typeInfo;
924   const TreeNode* templateNode = NULL;
925
926   if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
927   {
928     typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
929
930     if( !typeInfo )
931     {
932       // a template name is also allowed inplace of the type name
933       OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
934
935       if( templates )
936       {
937         if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
938         {
939           templateNode = &(*isTemplate);
940
941           if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
942           {
943             typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
944           }
945         }
946       }
947     }
948   }
949
950   if(!typeInfo)
951   {
952     DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
953   }
954   else
955   {
956     baseHandle       = typeInfo.CreateInstance();
957     Handle handle    = Handle::DownCast(baseHandle);
958     Actor actor      = Actor::DownCast(handle);
959
960     if(handle)
961     {
962
963       DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
964
965 #if defined(DEBUG_ENABLED)
966       if(handle)
967       {
968         DALI_SCRIPT_VERBOSE("  Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
969         DALI_SCRIPT_VERBOSE("  Is Handle Property Count=%d\n", handle.GetPropertyCount());
970       }
971
972       if(actor)
973       {
974         DALI_SCRIPT_VERBOSE("  Is Actor id=%d\n", actor.GetId());
975       }
976
977       Toolkit::Control control  = Toolkit::Control::DownCast(handle);
978       if(control)
979       {
980         DALI_SCRIPT_VERBOSE("  Is Control id=%d\n", actor.GetId());
981       }
982 #endif // DEBUG_ENABLED
983
984       if( templateNode )
985       {
986         ApplyProperties( root, *templateNode, handle, replacements );
987
988         if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
989         {
990           for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
991           {
992             DoCreate( root, (*iter).second, actor, replacements );
993           }
994         }
995       }
996
997       if( actor )
998       {
999         // add children of all the styles
1000         if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
1001         {
1002           for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
1003           {
1004             DoCreate( root, (*iter).second, actor, replacements );
1005           }
1006         }
1007
1008         // apply style on top as they need the children to exist
1009         ApplyAllStyleProperties( root, node, actor, replacements );
1010
1011         // then add to parent
1012         if( parent )
1013         {
1014           parent.Add( actor );
1015         }
1016       }
1017       else
1018       {
1019         ApplyProperties( root, node, handle, replacements );
1020       }
1021     }
1022     else
1023     {
1024       DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
1025     }
1026   }
1027
1028   return baseHandle;
1029 }
1030
1031 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
1032 {
1033   const Stage& stage = Stage::GetCurrent();
1034   Layer root  = stage.GetRootLayer();
1035
1036   if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
1037   {
1038     Actor actor = root.FindChildByName(*s);
1039     if(actor)
1040     {
1041       task.SetSourceActor( actor );
1042     }
1043     else
1044     {
1045       DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
1046     }
1047   }
1048
1049   if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
1050   {
1051     CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
1052     if(actor)
1053     {
1054       task.SetCameraActor( actor );
1055     }
1056     else
1057     {
1058       DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
1059     }
1060   }
1061
1062   if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
1063   {
1064     FrameBufferImage fb = GetFrameBufferImage( *s, constant );
1065     if(fb)
1066     {
1067       task.SetTargetFrameBuffer( fb );
1068     }
1069     else
1070     {
1071       DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
1072     }
1073   }
1074
1075   if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
1076   {
1077     if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
1078     {
1079       task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
1080     }
1081     else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
1082     {
1083       task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
1084     }
1085     else
1086     {
1087       DALI_SCRIPT_WARNING("todo");
1088     }
1089   }
1090
1091   // other setup is via the property system
1092   SetProperties( node, task, constant );
1093 }
1094
1095 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1096 {
1097   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1098
1099   OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1100
1101   std::string styleNameLower(styleName);
1102   OptionalChild style  = IsChildIgnoreCase( *styles, styleNameLower );
1103
1104   if( styles && style )
1105   {
1106     ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1107     return true;
1108   }
1109   else
1110   {
1111     return false;
1112   }
1113 }
1114
1115 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
1116                                        Dali::Handle& handle, const Replacement& constant )
1117 {
1118   const char* styleName = node.GetName();
1119
1120   StylePtr style = Style::New();
1121
1122   StylePtr* matchedStyle = NULL;
1123   if( styleName )
1124   {
1125     matchedStyle = mStyles.Find( styleName );
1126     if( ! matchedStyle )
1127     {
1128       OptionalChild styleNodes = IsChild(root, KEYNAME_STYLES);
1129       OptionalChild inheritFromNode = IsChild(node, KEYNAME_INHERIT);
1130       if( !inheritFromNode )
1131       {
1132         inheritFromNode = IsChild( node, KEYNAME_STYLES );
1133       }
1134
1135       if( styleNodes )
1136       {
1137         if( inheritFromNode )
1138         {
1139           TreeNodeList additionalStyleNodes;
1140
1141           CollectAllStyles( *styleNodes, *inheritFromNode, additionalStyleNodes );
1142
1143 #if defined(DEBUG_ENABLED)
1144           for(TreeNode::ConstIterator iter = (*inheritFromNode).CBegin(); iter != (*inheritFromNode).CEnd(); ++iter)
1145           {
1146             if( OptionalString styleName = IsString( (*iter).second ) )
1147             {
1148               DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
1149             }
1150           }
1151 #endif
1152
1153           // a style may have other styles, which has other styles etc so we apply in reverse by convention.
1154           for(TreeNodeList::reverse_iterator iter = additionalStyleNodes.rbegin(); iter != additionalStyleNodes.rend(); ++iter)
1155           {
1156             RecordStyle( style, *(*iter), handle, constant );
1157             ApplySignals( root, *(*iter), handle );
1158             ApplyStylesByActor( root, *(*iter), handle, constant );
1159           }
1160         }
1161
1162         RecordStyle( style, node, handle, constant );
1163         mStyles.Add( styleName, style ); // shallow copy
1164         matchedStyle = &style;
1165       }
1166     }
1167   }
1168
1169   if( matchedStyle )
1170   {
1171     StylePtr style( *matchedStyle );
1172     Dictionary<Property::Map> instancedProperties;
1173     style->ApplyVisualsAndPropertiesRecursively( handle, instancedProperties );
1174   }
1175   else // If there were no styles, instead set properties
1176   {
1177     SetProperties( node, handle, constant );
1178   }
1179   ApplySignals( root, node, handle );
1180   ApplyStylesByActor( root, node, handle, constant );
1181 }
1182
1183 void Builder::RecordStyle( StylePtr           style,
1184                            const TreeNode&    node,
1185                            Dali::Handle&      handle,
1186                            const Replacement& replacements )
1187 {
1188   // With repeated calls, accumulate inherited states, visuals and properties
1189   // but override any with same name
1190
1191   for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
1192   {
1193     const TreeNode::KeyNodePair& keyValue = *iter;
1194     std::string key( keyValue.first );
1195     if( key == KEYNAME_STATES )
1196     {
1197       const TreeNode& states = keyValue.second;
1198       if( states.GetType() != TreeNode::OBJECT )
1199       {
1200         DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON object\n", key.c_str() );
1201         continue;
1202       }
1203
1204       for( TreeNode::ConstIterator iter = states.CBegin(); iter != states.CEnd(); ++iter )
1205       {
1206         const TreeNode& stateNode = (*iter).second;
1207         const char* stateName = stateNode.GetName();
1208         if( stateNode.GetType() != TreeNode::OBJECT )
1209         {
1210           DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON object\n", stateName );
1211           continue;
1212         }
1213
1214         StylePtr* stylePtr = style->subStates.Find( stateName );
1215         if( stylePtr )
1216         {
1217           StylePtr style(*stylePtr);
1218           RecordStyle( style, stateNode, handle, replacements );
1219         }
1220         else
1221         {
1222           StylePtr subState = Style::New();
1223           RecordStyle( subState, stateNode, handle, replacements );
1224           style->subStates.Add( stateName, subState );
1225         }
1226       }
1227     }
1228     else if( key == KEYNAME_VISUALS )
1229     {
1230       for( TreeNode::ConstIterator iter = keyValue.second.CBegin(); iter != keyValue.second.CEnd(); ++iter )
1231       {
1232         // Each key in this table should be a property name matching a visual.
1233         const TreeNode::KeyNodePair& visual = *iter;
1234         Dali::Property::Value property(Property::MAP);
1235         if( DeterminePropertyFromNode( visual.second, Property::MAP, property, replacements ) )
1236         {
1237           Property::Map* mapPtr = style->visuals.Find( visual.first );
1238           if( mapPtr )
1239           {
1240             // Override existing visuals
1241             mapPtr->Clear();
1242             mapPtr->Merge( *property.GetMap() );
1243           }
1244           else
1245           {
1246             style->visuals.Add(visual.first, *property.GetMap());
1247           }
1248         }
1249       }
1250     }
1251     else if( key == KEYNAME_ENTRY_TRANSITION )
1252     {
1253       RecordTransitionData( keyValue, style->entryTransition, replacements );
1254     }
1255     else if( key == KEYNAME_EXIT_TRANSITION )
1256     {
1257       RecordTransitionData( keyValue, style->exitTransition, replacements );
1258     }
1259     else if( key == KEYNAME_TRANSITIONS )
1260     {
1261       RecordTransitions( keyValue, style->transitions, replacements );
1262     }
1263     else if( key == KEYNAME_TYPE ||
1264              key == KEYNAME_ACTORS ||
1265              key == KEYNAME_SIGNALS ||
1266              key == KEYNAME_STYLES ||
1267              key == KEYNAME_MAPPINGS ||
1268              key == KEYNAME_INHERIT )
1269     {
1270       continue;
1271     }
1272     else // It's a property
1273     {
1274       Property::Index index;
1275       Property::Value value;
1276       if( MapToTargetProperty( handle, key, keyValue.second, replacements, index, value ) )
1277       {
1278         Property::Value* existingValuePtr = style->properties.Find( index );
1279         if( existingValuePtr != NULL )
1280         {
1281           *existingValuePtr = value; // Overwrite existing property.
1282         }
1283         else
1284         {
1285           style->properties.Add( index, value );
1286         }
1287       }
1288     }
1289   }
1290 }
1291
1292 void Builder::RecordTransitions(
1293   const TreeNode::KeyNodePair& keyValue,
1294   Property::Array& value,
1295   const Replacement& replacements )
1296 {
1297   //@todo add new transitions to style.transitions
1298   //      override existing transitions. A transition matches on target & property name
1299   const TreeNode& node = keyValue.second;
1300   if( node.GetType() == TreeNode::ARRAY )
1301   {
1302     Dali::Property::Value property(Property::ARRAY);
1303     if( DeterminePropertyFromNode( node, Property::ARRAY, property, replacements ) )
1304     {
1305       value = *property.GetArray();
1306     }
1307   }
1308   else if( node.GetType() == TreeNode::OBJECT )
1309   {
1310     Dali::Property::Value property(Property::MAP);
1311     if( DeterminePropertyFromNode( node, Property::MAP, property, replacements ) )
1312     {
1313       Property::Array propertyArray;
1314       propertyArray.Add( property );
1315       value = propertyArray;
1316     }
1317   }
1318   else
1319   {
1320     DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON array or object\n", keyValue.first );
1321   }
1322 }
1323
1324 void Builder::RecordTransitionData(
1325   const TreeNode::KeyNodePair& keyValue,
1326   Toolkit::TransitionData& transitionData,
1327   const Replacement& replacements )
1328 {
1329   const TreeNode& node = keyValue.second;
1330   if( node.GetType() == TreeNode::ARRAY )
1331   {
1332     Dali::Property::Value property(Property::ARRAY);
1333     if( DeterminePropertyFromNode( keyValue.second, Property::ARRAY, property, replacements ) )
1334     {
1335       transitionData = Toolkit::TransitionData::New( *property.GetArray() );
1336     }
1337   }
1338   else if( node.GetType() == TreeNode::OBJECT )
1339   {
1340     Dali::Property::Value property(Property::MAP);
1341     if( DeterminePropertyFromNode( keyValue.second, Property::MAP, property, replacements ) )
1342     {
1343       transitionData = Toolkit::TransitionData::New( *property.GetMap() );
1344     }
1345   }
1346 }
1347
1348
1349 // Set properties from node on handle.
1350 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
1351                                Dali::Handle& handle, const Replacement& constant )
1352 {
1353   SetProperties( node, handle, constant );
1354   ApplySignals( root, node, handle );
1355 }
1356
1357 void Builder::ApplySignals(const TreeNode& root, const TreeNode& node, Dali::Handle& handle )
1358 {
1359   Actor actor = Actor::DownCast(handle);
1360   if( actor )
1361   {
1362     // add signals
1363     SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
1364     SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
1365   }
1366 }
1367
1368
1369 // Appling by style helper
1370 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
1371 void Builder::ApplyStylesByActor(  const TreeNode& root, const TreeNode& node,
1372                                    Dali::Handle& handle, const Replacement& constant )
1373 {
1374   if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
1375   {
1376     if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
1377     {
1378       // in a style the actor subtree properties referenced by actor name
1379       for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
1380       {
1381         Dali::Actor foundActor;
1382
1383         if( (*iter).first )
1384         {
1385           foundActor = actor.FindChildByName( (*iter).first );
1386         }
1387
1388         if( !foundActor )
1389         {
1390           DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
1391         }
1392         else
1393         {
1394           DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
1395           ApplyProperties( root, (*iter).second, foundActor, constant );
1396         }
1397       }
1398     }
1399   }
1400 }
1401
1402 /*
1403  * Sets the handle properties found in the tree node
1404  */
1405 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
1406 {
1407   if( handle )
1408   {
1409     for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
1410     {
1411       const TreeNode::KeyNodePair& keyChild = *iter;
1412
1413       std::string key( keyChild.first );
1414
1415       // ignore special fields;
1416       if( key == KEYNAME_TYPE ||
1417           key == KEYNAME_ACTORS ||
1418           key == KEYNAME_SIGNALS ||
1419           key == KEYNAME_STYLES ||
1420           key == KEYNAME_MAPPINGS ||
1421           key == KEYNAME_INHERIT ||
1422           key == KEYNAME_STATES ||
1423           key == KEYNAME_VISUALS ||
1424           key == KEYNAME_ENTRY_TRANSITION ||
1425           key == KEYNAME_EXIT_TRANSITION ||
1426           key == KEYNAME_TRANSITIONS )
1427       {
1428         continue;
1429       }
1430
1431       Property::Index index;
1432       Property::Value value;
1433
1434       bool mapped = MapToTargetProperty( handle, key, keyChild.second, constant, index, value );
1435       if( mapped )
1436       {
1437         DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
1438
1439         handle.SetProperty( index, value );
1440       }
1441
1442       // Add custom properties
1443       SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE);
1444       SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE);
1445
1446     } // for property nodes
1447   }
1448   else
1449   {
1450     DALI_SCRIPT_WARNING("Style applied to empty handle\n");
1451   }
1452 }
1453
1454 bool Builder::MapToTargetProperty(
1455   Handle&            propertyObject,
1456   const std::string& key,
1457   const TreeNode&    node,
1458   const Replacement& constant,
1459   Property::Index&   index,
1460   Property::Value&   value )
1461 {
1462   bool mapped = false;
1463
1464   index = propertyObject.GetPropertyIndex( key );
1465   if( Property::INVALID_INDEX != index )
1466   {
1467     Property::Type type = propertyObject.GetPropertyType(index);
1468
1469     // if node.value is a mapping, get the property value from the "mappings" table
1470     if( node.GetType() == TreeNode::STRING )
1471     {
1472       std::string mappingKey;
1473       if( GetMappingKey( node.GetString(), mappingKey) )
1474       {
1475         OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS );
1476         mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value );
1477       }
1478     }
1479     if( ! mapped )
1480     {
1481       mapped = DeterminePropertyFromNode( node, type, value, constant );
1482       if( ! mapped )
1483       {
1484         // Just determine the property from the node and if it's valid, let the property object handle it
1485         DeterminePropertyFromNode( node, value, constant );
1486         mapped = ( value.GetType() != Property::NONE );
1487       }
1488     }
1489   }
1490   else
1491   {
1492     DALI_LOG_ERROR("Key '%s' not found.\n", key.c_str());
1493   }
1494   return mapped;
1495 }
1496
1497 bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value )
1498 {
1499   KeyStack keyStack;
1500   return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value );
1501 }
1502
1503 bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value )
1504 {
1505   Replacement replacer( mReplacementMap );
1506   bool result = false;
1507
1508   keyStack.push_back( theKey );
1509
1510   for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter )
1511   {
1512     std::string aKey( (*iter).first );
1513     if( aKey.compare( theKey ) == 0 )
1514     {
1515       if( propertyType == Property::NONE )
1516       {
1517         DeterminePropertyFromNode( (*iter).second, value, replacer );
1518         result = true;
1519       }
1520       else
1521       {
1522         result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer );
1523       }
1524
1525       if( result )
1526       {
1527         ConvertChildValue(mappingRoot, keyStack, value);
1528       }
1529       break;
1530     }
1531   }
1532   keyStack.pop_back();
1533
1534   return result;
1535 }
1536
1537 bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child )
1538 {
1539   bool result = false;
1540
1541   switch( child.GetType() )
1542   {
1543     case Property::STRING:
1544     {
1545       std::string value;
1546       if( child.Get( value ) )
1547       {
1548         std::string key;
1549         if( GetMappingKey( value, key ) )
1550         {
1551           // Check key for cycles:
1552           result=true;
1553           for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter )
1554           {
1555             if( key.compare(*iter) == 0 )
1556             {
1557               // key is already in stack; stop.
1558               DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str());
1559               child = Property::Value("");
1560               result=false;
1561               break;
1562             }
1563           }
1564
1565           if( result )
1566           {
1567             // The following call will overwrite the child with the value
1568             // from the mapping.
1569             RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child );
1570             result = true;
1571           }
1572         }
1573       }
1574       break;
1575     }
1576
1577     case Property::MAP:
1578     {
1579       Property::Map* map = child.GetMap();
1580       if( map )
1581       {
1582         for( Property::Map::SizeType i=0; i < map->Count(); ++i )
1583         {
1584           Property::Value& child = map->GetValue(i);
1585           ConvertChildValue(mappingRoot, keyStack, child);
1586         }
1587       }
1588       break;
1589     }
1590
1591     case Property::ARRAY:
1592     {
1593       Property::Array* array = child.GetArray();
1594       if( array )
1595       {
1596         for( Property::Array::SizeType i=0; i < array->Count(); ++i )
1597         {
1598           Property::Value& child = array->GetElementAt(i);
1599           ConvertChildValue(mappingRoot, keyStack, child);
1600         }
1601       }
1602       break;
1603     }
1604
1605     default:
1606       // Ignore other types.
1607       break;
1608   }
1609
1610   return result;
1611 }
1612
1613 void Builder::SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant,
1614                           const std::string& childName, Property::AccessMode accessMode )
1615 {
1616   // Add custom properties
1617   if( OptionalChild customPropertiesChild = IsChild(node, childName) )
1618   {
1619     const TreeNode& customPropertiesNode = *customPropertiesChild;
1620     const TreeConstIter endIter = customPropertiesNode.CEnd();
1621     for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter )
1622     {
1623       const TreeNode::KeyNodePair& keyChild = *iter;
1624       std::string key( keyChild.first );
1625       Property::Value value;
1626       DeterminePropertyFromNode( keyChild.second, value, constant );
1627
1628       // Register/Set property.
1629       handle.RegisterProperty( key, value, accessMode );
1630     }
1631   }
1632 }
1633
1634
1635 } // namespace Internal
1636
1637 } // namespace Toolkit
1638
1639 } // namespace Dali