add drag&drop support in actor level
[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 constant map (allows the user to override the constants in the json after loading)
183     LoadConstants( *parser.GetRoot(), mReplacementMap );
184     // load configuration map
185     LoadConfiguration( *parser.GetRoot(), mConfigurationMap );
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
801         // If config is string, find constant and replace it to original value.
802         if( (*iter).second.GetType() == TreeNode::STRING )
803         {
804           std::string stringConfigValue;
805           if( property.Get( stringConfigValue ) )
806           {
807             std::size_t pos = 0;
808
809             while( pos < stringConfigValue.size() )
810             {
811               // If we can't find "{","}" pair in stringConfigValue, will out loop.
812               std::size_t leftPos = stringConfigValue.find( "{", pos );
813               if( leftPos != std::string::npos )
814               {
815                 std::size_t rightPos = stringConfigValue.find( "}", pos+1 );
816
817                 if( rightPos != std::string::npos )
818                 {
819                   // If we find "{","}" pair but can't find matched constant
820                   // try to find other "{","}" pair after current left position.
821                   pos = leftPos+1;
822
823                   for( unsigned int i = 0; i < mReplacementMap.Count() ; i++ )
824                   {
825                     std::string constant = mReplacementMap.GetKey(i);
826
827                     // Compare string which is between "{" and "}" with constant string
828                     // If they are same, change string in stringConfigValue to mapped constant value.
829                     if ( stringConfigValue.compare( leftPos+1, rightPos-leftPos-1,constant) == 0 )
830                     {
831                       std::string replaceString;
832                       mReplacementMap.GetValue(i).Get( replaceString );
833
834                       stringConfigValue.replace( leftPos, rightPos-leftPos+1, replaceString );
835                       pos = leftPos + replaceString.size();
836                       break;
837                     }
838                   }
839                 }
840                 else
841                 {
842                   // If we cannot find constant in const value, will out loop.
843                   pos = stringConfigValue.size();
844                 }
845               }
846               else
847               {
848                 // If we cannot find constant in const value, will out loop.
849                 pos = stringConfigValue.size();
850               }
851             }
852             property = Property::Value( stringConfigValue );
853           }
854         }
855         intoMap[ (*iter).second.GetName() ] = property;
856       }
857     }
858   }
859 }
860
861 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
862 {
863   Replacement replacer(intoMap);
864
865   if( OptionalChild constants = IsChild(root, "constants") )
866   {
867     for(TreeNode::ConstIterator iter = (*constants).CBegin();
868         iter != (*constants).CEnd(); ++iter)
869     {
870       Dali::Property::Value property;
871       if( (*iter).second.GetName() )
872       {
873 #if defined(DEBUG_ENABLED)
874         DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
875 #endif
876         DeterminePropertyFromNode( (*iter).second, property, replacer );
877         intoMap[ (*iter).second.GetName() ] = property;
878       }
879     }
880   }
881
882 #if defined(DEBUG_ENABLED)
883   Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
884   if( iter && iter->GetType() == Property::STRING )
885   {
886     std::string logLevel( iter->Get< std::string >() );
887     if( logLevel == "NoLogging" )
888     {
889       gFilterScript->SetLogLevel( Integration::Log::NoLogging );
890     }
891     else if( logLevel == "Concise" )
892     {
893       gFilterScript->SetLogLevel( Integration::Log::Concise );
894     }
895     else if( logLevel == "General" )
896     {
897       gFilterScript->SetLogLevel( Integration::Log::General );
898     }
899     else if( logLevel == "Verbose" )
900     {
901       gFilterScript->SetLogLevel( Integration::Log::Verbose );
902     }
903   }
904 #endif
905 }
906
907 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
908 {
909   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
910
911   Animation anim;
912
913   if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
914   {
915     if( OptionalChild animation = IsChild(*animations, animationName) )
916     {
917       anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
918     }
919     else
920     {
921       DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
922     }
923   }
924   else
925   {
926     DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
927   }
928
929   return anim;
930 }
931
932 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
933 {
934   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
935
936   BaseHandle baseHandle;
937
938   OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
939
940   if( !templates )
941   {
942     DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
943   }
944   else
945   {
946     OptionalChild childTemplate = IsChild(*templates, templateName);
947     if(!childTemplate)
948     {
949       DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
950     }
951     else
952     {
953       OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
954
955       if(!type)
956       {
957         DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
958       }
959       else
960       {
961         baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
962       }
963     }
964   }
965
966   return baseHandle;
967 }
968
969 /*
970  * Create a dali type from a node.
971  * If parent given and an actor type was created then add it to the parent and
972  * recursively add nodes children.
973  */
974 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
975                               Actor parent, const Replacement& replacements )
976 {
977   BaseHandle baseHandle;
978   TypeInfo typeInfo;
979   const TreeNode* templateNode = NULL;
980
981   if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
982   {
983     typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
984
985     if( !typeInfo )
986     {
987       // a template name is also allowed inplace of the type name
988       OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
989
990       if( templates )
991       {
992         if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
993         {
994           templateNode = &(*isTemplate);
995
996           if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
997           {
998             typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
999           }
1000         }
1001       }
1002     }
1003   }
1004
1005   if(!typeInfo)
1006   {
1007     DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
1008   }
1009   else
1010   {
1011     baseHandle       = typeInfo.CreateInstance();
1012     Handle handle    = Handle::DownCast(baseHandle);
1013     Actor actor      = Actor::DownCast(handle);
1014
1015     if(handle)
1016     {
1017
1018       DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
1019
1020 #if defined(DEBUG_ENABLED)
1021       if(handle)
1022       {
1023         DALI_SCRIPT_VERBOSE("  Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
1024         DALI_SCRIPT_VERBOSE("  Is Handle Property Count=%d\n", handle.GetPropertyCount());
1025       }
1026
1027       if(actor)
1028       {
1029         DALI_SCRIPT_VERBOSE("  Is Actor id=%d\n", actor.GetId());
1030       }
1031
1032       Toolkit::Control control  = Toolkit::Control::DownCast(handle);
1033       if(control)
1034       {
1035         DALI_SCRIPT_VERBOSE("  Is Control id=%d\n", actor.GetId());
1036       }
1037 #endif // DEBUG_ENABLED
1038
1039       if( templateNode )
1040       {
1041         ApplyProperties( root, *templateNode, handle, replacements );
1042
1043         if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
1044         {
1045           for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
1046           {
1047             DoCreate( root, (*iter).second, actor, replacements );
1048           }
1049         }
1050       }
1051
1052       if( actor )
1053       {
1054         // add children of all the styles
1055         if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
1056         {
1057           for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
1058           {
1059             DoCreate( root, (*iter).second, actor, replacements );
1060           }
1061         }
1062
1063         // apply style on top as they need the children to exist
1064         ApplyAllStyleProperties( root, node, actor, replacements );
1065
1066         // then add to parent
1067         if( parent )
1068         {
1069           parent.Add( actor );
1070         }
1071       }
1072       else
1073       {
1074         ApplyProperties( root, node, handle, replacements );
1075       }
1076     }
1077     else
1078     {
1079       DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
1080     }
1081   }
1082
1083   return baseHandle;
1084 }
1085
1086 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
1087 {
1088   const Stage& stage = Stage::GetCurrent();
1089   Layer root  = stage.GetRootLayer();
1090
1091   if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
1092   {
1093     Actor actor = root.FindChildByName(*s);
1094     if(actor)
1095     {
1096       task.SetSourceActor( actor );
1097     }
1098     else
1099     {
1100       DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
1101     }
1102   }
1103
1104   if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
1105   {
1106     CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
1107     if(actor)
1108     {
1109       task.SetCameraActor( actor );
1110     }
1111     else
1112     {
1113       DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
1114     }
1115   }
1116
1117   if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
1118   {
1119     FrameBufferImage fb = GetFrameBufferImage( *s, constant );
1120     if(fb)
1121     {
1122       task.SetTargetFrameBuffer( fb );
1123     }
1124     else
1125     {
1126       DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
1127     }
1128   }
1129
1130   if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
1131   {
1132     if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
1133     {
1134       task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
1135     }
1136     else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
1137     {
1138       task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
1139     }
1140     else
1141     {
1142       DALI_SCRIPT_WARNING("todo");
1143     }
1144   }
1145
1146   // other setup is via the property system
1147   SetProperties( node, task, constant );
1148 }
1149
1150 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1151 {
1152   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1153
1154   OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1155
1156   std::string styleNameLower(styleName);
1157   OptionalChild style  = IsChildIgnoreCase( *styles, styleNameLower );
1158
1159   if( styles && style )
1160   {
1161     ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1162     return true;
1163   }
1164   else
1165   {
1166     return false;
1167   }
1168 }
1169
1170 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
1171                                        Dali::Handle& handle, const Replacement& constant )
1172 {
1173   const char* styleName = node.GetName();
1174
1175   StylePtr style = Style::New();
1176
1177   StylePtr* matchedStyle = NULL;
1178   if( styleName )
1179   {
1180     matchedStyle = mStyles.Find( styleName );
1181     if( ! matchedStyle )
1182     {
1183       OptionalChild styleNodes = IsChild(root, KEYNAME_STYLES);
1184       OptionalChild inheritFromNode = IsChild(node, KEYNAME_INHERIT);
1185       if( !inheritFromNode )
1186       {
1187         inheritFromNode = IsChild( node, KEYNAME_STYLES );
1188       }
1189
1190       if( styleNodes )
1191       {
1192         if( inheritFromNode )
1193         {
1194           TreeNodeList additionalStyleNodes;
1195
1196           CollectAllStyles( *styleNodes, *inheritFromNode, additionalStyleNodes );
1197
1198 #if defined(DEBUG_ENABLED)
1199           for(TreeNode::ConstIterator iter = (*inheritFromNode).CBegin(); iter != (*inheritFromNode).CEnd(); ++iter)
1200           {
1201             if( OptionalString styleName = IsString( (*iter).second ) )
1202             {
1203               DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
1204             }
1205           }
1206 #endif
1207
1208           // a style may have other styles, which has other styles etc so we apply in reverse by convention.
1209           for(TreeNodeList::reverse_iterator iter = additionalStyleNodes.rbegin(); iter != additionalStyleNodes.rend(); ++iter)
1210           {
1211             RecordStyle( style, *(*iter), handle, constant );
1212             ApplySignals( root, *(*iter), handle );
1213             ApplyStylesByActor( root, *(*iter), handle, constant );
1214           }
1215         }
1216
1217         RecordStyle( style, node, handle, constant );
1218         mStyles.Add( styleName, style ); // shallow copy
1219         matchedStyle = &style;
1220       }
1221     }
1222   }
1223
1224   if( matchedStyle )
1225   {
1226     StylePtr style( *matchedStyle );
1227     Dictionary<Property::Map> instancedProperties;
1228     style->ApplyVisualsAndPropertiesRecursively( handle, instancedProperties );
1229   }
1230   else // If there were no styles, instead set properties
1231   {
1232     SetProperties( node, handle, constant );
1233   }
1234   ApplySignals( root, node, handle );
1235   ApplyStylesByActor( root, node, handle, constant );
1236 }
1237
1238 void Builder::RecordStyle( StylePtr           style,
1239                            const TreeNode&    node,
1240                            Dali::Handle&      handle,
1241                            const Replacement& replacements )
1242 {
1243   // With repeated calls, accumulate inherited states, visuals and properties
1244   // but override any with same name
1245
1246   for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
1247   {
1248     const TreeNode::KeyNodePair& keyValue = *iter;
1249     std::string key( keyValue.first );
1250     if( key == KEYNAME_STATES )
1251     {
1252       const TreeNode& states = keyValue.second;
1253       if( states.GetType() != TreeNode::OBJECT )
1254       {
1255         DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON object\n", key.c_str() );
1256         continue;
1257       }
1258
1259       for( TreeNode::ConstIterator iter = states.CBegin(); iter != states.CEnd(); ++iter )
1260       {
1261         const TreeNode& stateNode = (*iter).second;
1262         const char* stateName = stateNode.GetName();
1263         if( stateNode.GetType() != TreeNode::OBJECT )
1264         {
1265           DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON object\n", stateName );
1266           continue;
1267         }
1268
1269         StylePtr* stylePtr = style->subStates.Find( stateName );
1270         if( stylePtr )
1271         {
1272           StylePtr style(*stylePtr);
1273           RecordStyle( style, stateNode, handle, replacements );
1274         }
1275         else
1276         {
1277           StylePtr subState = Style::New();
1278           RecordStyle( subState, stateNode, handle, replacements );
1279           style->subStates.Add( stateName, subState );
1280         }
1281       }
1282     }
1283     else if( key == KEYNAME_VISUALS )
1284     {
1285       for( TreeNode::ConstIterator iter = keyValue.second.CBegin(); iter != keyValue.second.CEnd(); ++iter )
1286       {
1287         // Each key in this table should be a property name matching a visual.
1288         const TreeNode::KeyNodePair& visual = *iter;
1289         Dali::Property::Value property(Property::MAP);
1290         if( DeterminePropertyFromNode( visual.second, Property::MAP, property, replacements ) )
1291         {
1292           Property::Map* mapPtr = style->visuals.Find( visual.first );
1293           if( mapPtr )
1294           {
1295             // Override existing visuals
1296             mapPtr->Clear();
1297             mapPtr->Merge( *property.GetMap() );
1298           }
1299           else
1300           {
1301             Property::Map* map = property.GetMap();
1302             if( map )
1303             {
1304               style->visuals.Add( visual.first, *map );
1305             }
1306           }
1307         }
1308       }
1309     }
1310     else if( key == KEYNAME_ENTRY_TRANSITION )
1311     {
1312       RecordTransitionData( keyValue, style->entryTransition, replacements );
1313     }
1314     else if( key == KEYNAME_EXIT_TRANSITION )
1315     {
1316       RecordTransitionData( keyValue, style->exitTransition, replacements );
1317     }
1318     else if( key == KEYNAME_TRANSITIONS )
1319     {
1320       RecordTransitions( keyValue, style->transitions, replacements );
1321     }
1322     else if( key == KEYNAME_TYPE ||
1323              key == KEYNAME_ACTORS ||
1324              key == KEYNAME_SIGNALS ||
1325              key == KEYNAME_STYLES ||
1326              key == KEYNAME_MAPPINGS ||
1327              key == KEYNAME_INHERIT )
1328     {
1329       continue;
1330     }
1331     else // It's a property
1332     {
1333       Property::Index index;
1334       Property::Value value;
1335       if( MapToTargetProperty( handle, key, keyValue.second, replacements, index, value ) )
1336       {
1337         Property::Value* existingValuePtr = style->properties.Find( index );
1338         if( existingValuePtr != NULL )
1339         {
1340           *existingValuePtr = value; // Overwrite existing property.
1341         }
1342         else
1343         {
1344           style->properties.Add( index, value );
1345         }
1346       }
1347     }
1348   }
1349 }
1350
1351 void Builder::RecordTransitions(
1352   const TreeNode::KeyNodePair& keyValue,
1353   Property::Array& value,
1354   const Replacement& replacements )
1355 {
1356   //@todo add new transitions to style.transitions
1357   //      override existing transitions. A transition matches on target & property name
1358   const TreeNode& node = keyValue.second;
1359   if( node.GetType() == TreeNode::ARRAY )
1360   {
1361     Dali::Property::Value property(Property::ARRAY);
1362     if( DeterminePropertyFromNode( node, Property::ARRAY, property, replacements ) )
1363     {
1364       value = *property.GetArray();
1365     }
1366   }
1367   else if( node.GetType() == TreeNode::OBJECT )
1368   {
1369     Dali::Property::Value property(Property::MAP);
1370     if( DeterminePropertyFromNode( node, Property::MAP, property, replacements ) )
1371     {
1372       Property::Array propertyArray;
1373       propertyArray.Add( property );
1374       value = propertyArray;
1375     }
1376   }
1377   else
1378   {
1379     DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON array or object\n", keyValue.first );
1380   }
1381 }
1382
1383 void Builder::RecordTransitionData(
1384   const TreeNode::KeyNodePair& keyValue,
1385   Toolkit::TransitionData& transitionData,
1386   const Replacement& replacements )
1387 {
1388   const TreeNode& node = keyValue.second;
1389   if( node.GetType() == TreeNode::ARRAY )
1390   {
1391     Dali::Property::Value property(Property::ARRAY);
1392     if( DeterminePropertyFromNode( keyValue.second, Property::ARRAY, property, replacements ) )
1393     {
1394       transitionData = Toolkit::TransitionData::New( *property.GetArray() );
1395     }
1396   }
1397   else if( node.GetType() == TreeNode::OBJECT )
1398   {
1399     Dali::Property::Value property(Property::MAP);
1400     if( DeterminePropertyFromNode( keyValue.second, Property::MAP, property, replacements ) )
1401     {
1402       transitionData = Toolkit::TransitionData::New( *property.GetMap() );
1403     }
1404   }
1405 }
1406
1407
1408 // Set properties from node on handle.
1409 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
1410                                Dali::Handle& handle, const Replacement& constant )
1411 {
1412   SetProperties( node, handle, constant );
1413   ApplySignals( root, node, handle );
1414 }
1415
1416 void Builder::ApplySignals(const TreeNode& root, const TreeNode& node, Dali::Handle& handle )
1417 {
1418   Actor actor = Actor::DownCast(handle);
1419   if( actor )
1420   {
1421     // add signals
1422     SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
1423     SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
1424   }
1425 }
1426
1427
1428 // Appling by style helper
1429 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
1430 void Builder::ApplyStylesByActor(  const TreeNode& root, const TreeNode& node,
1431                                    Dali::Handle& handle, const Replacement& constant )
1432 {
1433   if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
1434   {
1435     if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
1436     {
1437       // in a style the actor subtree properties referenced by actor name
1438       for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
1439       {
1440         Dali::Actor foundActor;
1441
1442         if( (*iter).first )
1443         {
1444           foundActor = actor.FindChildByName( (*iter).first );
1445         }
1446
1447         if( !foundActor )
1448         {
1449           DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
1450         }
1451         else
1452         {
1453           DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
1454           ApplyProperties( root, (*iter).second, foundActor, constant );
1455         }
1456       }
1457     }
1458   }
1459 }
1460
1461 /*
1462  * Sets the handle properties found in the tree node
1463  */
1464 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
1465 {
1466   if( handle )
1467   {
1468     for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
1469     {
1470       const TreeNode::KeyNodePair& keyChild = *iter;
1471
1472       std::string key( keyChild.first );
1473
1474       // ignore special fields;
1475       if( key == KEYNAME_TYPE ||
1476           key == KEYNAME_ACTORS ||
1477           key == KEYNAME_SIGNALS ||
1478           key == KEYNAME_STYLES ||
1479           key == KEYNAME_MAPPINGS ||
1480           key == KEYNAME_INHERIT ||
1481           key == KEYNAME_STATES ||
1482           key == KEYNAME_VISUALS ||
1483           key == KEYNAME_ENTRY_TRANSITION ||
1484           key == KEYNAME_EXIT_TRANSITION ||
1485           key == KEYNAME_TRANSITIONS )
1486       {
1487         continue;
1488       }
1489
1490       Property::Index index;
1491       Property::Value value;
1492
1493       bool mapped = MapToTargetProperty( handle, key, keyChild.second, constant, index, value );
1494       if( mapped )
1495       {
1496         DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
1497
1498         handle.SetProperty( index, value );
1499       }
1500
1501       // Add custom properties
1502       SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE);
1503       SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE);
1504
1505     } // for property nodes
1506   }
1507   else
1508   {
1509     DALI_SCRIPT_WARNING("Style applied to empty handle\n");
1510   }
1511 }
1512
1513 bool Builder::MapToTargetProperty(
1514   Handle&            propertyObject,
1515   const std::string& key,
1516   const TreeNode&    node,
1517   const Replacement& constant,
1518   Property::Index&   index,
1519   Property::Value&   value )
1520 {
1521   bool mapped = false;
1522
1523   index = propertyObject.GetPropertyIndex( key );
1524   if( Property::INVALID_INDEX != index )
1525   {
1526     Property::Type type = propertyObject.GetPropertyType(index);
1527
1528     // if node.value is a mapping, get the property value from the "mappings" table
1529     if( node.GetType() == TreeNode::STRING )
1530     {
1531       std::string mappingKey;
1532       if( GetMappingKey( node.GetString(), mappingKey) )
1533       {
1534         OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS );
1535         mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value );
1536       }
1537     }
1538     if( ! mapped )
1539     {
1540       mapped = DeterminePropertyFromNode( node, type, value, constant );
1541       if( ! mapped )
1542       {
1543         // Just determine the property from the node and if it's valid, let the property object handle it
1544         DeterminePropertyFromNode( node, value, constant );
1545         mapped = ( value.GetType() != Property::NONE );
1546       }
1547     }
1548   }
1549   else
1550   {
1551     DALI_LOG_ERROR("Key '%s' not found.\n", key.c_str());
1552   }
1553   return mapped;
1554 }
1555
1556 bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value )
1557 {
1558   KeyStack keyStack;
1559   return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value );
1560 }
1561
1562 bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value )
1563 {
1564   Replacement replacer( mReplacementMap );
1565   bool result = false;
1566
1567   keyStack.push_back( theKey );
1568
1569   for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter )
1570   {
1571     std::string aKey( (*iter).first );
1572     if( aKey.compare( theKey ) == 0 )
1573     {
1574       if( propertyType == Property::NONE )
1575       {
1576         DeterminePropertyFromNode( (*iter).second, value, replacer );
1577         result = true;
1578       }
1579       else
1580       {
1581         result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer );
1582       }
1583
1584       if( result )
1585       {
1586         ConvertChildValue(mappingRoot, keyStack, value);
1587       }
1588       break;
1589     }
1590   }
1591   keyStack.pop_back();
1592
1593   return result;
1594 }
1595
1596 bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child )
1597 {
1598   bool result = false;
1599
1600   switch( child.GetType() )
1601   {
1602     case Property::STRING:
1603     {
1604       std::string value;
1605       if( child.Get( value ) )
1606       {
1607         std::string key;
1608         if( GetMappingKey( value, key ) )
1609         {
1610           // Check key for cycles:
1611           result=true;
1612           for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter )
1613           {
1614             if( key.compare(*iter) == 0 )
1615             {
1616               // key is already in stack; stop.
1617               DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str());
1618               child = Property::Value("");
1619               result=false;
1620               break;
1621             }
1622           }
1623
1624           if( result )
1625           {
1626             // The following call will overwrite the child with the value
1627             // from the mapping.
1628             RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child );
1629             result = true;
1630           }
1631         }
1632       }
1633       break;
1634     }
1635
1636     case Property::MAP:
1637     {
1638       Property::Map* map = child.GetMap();
1639       if( map )
1640       {
1641         for( Property::Map::SizeType i=0; i < map->Count(); ++i )
1642         {
1643           Property::Value& child = map->GetValue(i);
1644           ConvertChildValue(mappingRoot, keyStack, child);
1645         }
1646       }
1647       break;
1648     }
1649
1650     case Property::ARRAY:
1651     {
1652       Property::Array* array = child.GetArray();
1653       if( array )
1654       {
1655         for( Property::Array::SizeType i=0; i < array->Count(); ++i )
1656         {
1657           Property::Value& child = array->GetElementAt(i);
1658           ConvertChildValue(mappingRoot, keyStack, child);
1659         }
1660       }
1661       break;
1662     }
1663
1664     default:
1665       // Ignore other types.
1666       break;
1667   }
1668
1669   return result;
1670 }
1671
1672 void Builder::SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant,
1673                           const std::string& childName, Property::AccessMode accessMode )
1674 {
1675   // Add custom properties
1676   if( OptionalChild customPropertiesChild = IsChild(node, childName) )
1677   {
1678     const TreeNode& customPropertiesNode = *customPropertiesChild;
1679     const TreeConstIter endIter = customPropertiesNode.CEnd();
1680     for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter )
1681     {
1682       const TreeNode::KeyNodePair& keyChild = *iter;
1683       std::string key( keyChild.first );
1684       Property::Value value;
1685       DeterminePropertyFromNode( keyChild.second, value, constant );
1686
1687       // Register/Set property.
1688       handle.RegisterProperty( key, value, accessMode );
1689     }
1690   }
1691 }
1692
1693
1694 } // namespace Internal
1695
1696 } // namespace Toolkit
1697
1698 } // namespace Dali