Remove profile build dependencies
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / control / control-data-impl.cpp
1 /*
2  * Copyright (c) 2017 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 "control-data-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali/devel-api/scripting/enum-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/object/type-registry-helper.h>
29 #include <cstring>
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
33 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
34 #include <dali-toolkit/internal/styling/style-manager-impl.h>
35 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
36 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/devel-api/controls/control-devel.h>
39
40 namespace Dali
41 {
42
43 namespace Toolkit
44 {
45
46 namespace Internal
47 {
48
49 extern const Dali::Scripting::StringEnum ControlStateTable[];
50 extern const unsigned int ControlStateTableCount;
51
52
53 // Not static or anonymous - shared with other translation units
54 const Scripting::StringEnum ControlStateTable[] = {
55   { "NORMAL",   Toolkit::DevelControl::NORMAL   },
56   { "FOCUSED",  Toolkit::DevelControl::FOCUSED  },
57   { "DISABLED", Toolkit::DevelControl::DISABLED },
58 };
59 const unsigned int ControlStateTableCount = sizeof( ControlStateTable ) / sizeof( ControlStateTable[0] );
60
61
62
63 namespace
64 {
65
66 #if defined(DEBUG_ENABLED)
67 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
68 #endif
69
70
71 template<typename T>
72 void Remove( Dictionary<T>& keyValues, const std::string& name )
73 {
74   keyValues.Remove(name);
75 }
76
77 void Remove( DictionaryKeys& keys, const std::string& name )
78 {
79   DictionaryKeys::iterator iter = std::find( keys.begin(), keys.end(), name );
80   if( iter != keys.end())
81   {
82     keys.erase(iter);
83   }
84 }
85
86 Toolkit::DevelVisual::Type GetVisualTypeFromMap( const Property::Map& map )
87 {
88   Property::Value* typeValue = map.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE  );
89   Toolkit::DevelVisual::Type type = Toolkit::DevelVisual::IMAGE;
90   if( typeValue )
91   {
92     Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, type );
93   }
94   return type;
95 }
96
97 /**
98  *  Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
99  */
100 bool FindVisual( Property::Index targetIndex, const RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
101 {
102   for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
103   {
104     if ( (*iter)->index ==  targetIndex )
105     {
106       return true;
107     }
108   }
109   return false;
110 }
111
112 void FindChangableVisuals( Dictionary<Property::Map>& stateVisualsToAdd,
113                            Dictionary<Property::Map>& stateVisualsToChange,
114                            DictionaryKeys& stateVisualsToRemove)
115 {
116   DictionaryKeys copyOfStateVisualsToRemove = stateVisualsToRemove;
117
118   for( DictionaryKeys::iterator iter = copyOfStateVisualsToRemove.begin();
119        iter != copyOfStateVisualsToRemove.end(); ++iter )
120   {
121     const std::string& visualName = (*iter);
122     Property::Map* toMap = stateVisualsToAdd.Find( visualName );
123     if( toMap )
124     {
125       stateVisualsToChange.Add( visualName, *toMap );
126       stateVisualsToAdd.Remove( visualName );
127       Remove( stateVisualsToRemove, visualName );
128     }
129   }
130 }
131
132 Toolkit::Visual::Base GetVisualByName(
133   const RegisteredVisualContainer& visuals,
134   const std::string& visualName )
135 {
136   Toolkit::Visual::Base visualHandle;
137
138   RegisteredVisualContainer::Iterator iter;
139   for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
140   {
141     Toolkit::Visual::Base visual = (*iter)->visual;
142     if( visual && visual.GetName() == visualName )
143     {
144       visualHandle = visual;
145       break;
146     }
147   }
148   return visualHandle;
149 }
150
151 /**
152  * Performs actions as requested using the action name.
153  * @param[in] object The object on which to perform the action.
154  * @param[in] actionName The action to perform.
155  * @param[in] attributes The attributes with which to perfrom this action.
156  * @return true if action has been accepted by this control
157  */
158 const char* ACTION_ACCESSIBILITY_ACTIVATED = "accessibilityActivated";
159 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
160 {
161   bool ret = false;
162
163   if( object && ( 0 == strcmp( actionName.c_str(), ACTION_ACCESSIBILITY_ACTIVATED ) ) )
164   {
165     Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
166     if( control )
167     {
168       // if cast succeeds there is an implementation so no need to check
169       ret = Internal::GetImplementation( control ).OnAccessibilityActivated();
170     }
171   }
172
173   return ret;
174 }
175
176 /**
177  * Connects a callback function with the object's signals.
178  * @param[in] object The object providing the signal.
179  * @param[in] tracker Used to disconnect the signal.
180  * @param[in] signalName The signal to connect to.
181  * @param[in] functor A newly allocated FunctorDelegate.
182  * @return True if the signal was connected.
183  * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
184  */
185 const char* SIGNAL_KEY_EVENT = "keyEvent";
186 const char* SIGNAL_KEY_INPUT_FOCUS_GAINED = "keyInputFocusGained";
187 const char* SIGNAL_KEY_INPUT_FOCUS_LOST = "keyInputFocusLost";
188 const char* SIGNAL_TAPPED = "tapped";
189 const char* SIGNAL_PANNED = "panned";
190 const char* SIGNAL_PINCHED = "pinched";
191 const char* SIGNAL_LONG_PRESSED = "longPressed";
192 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
193 {
194   Dali::BaseHandle handle( object );
195
196   bool connected( false );
197   Toolkit::Control control = Toolkit::Control::DownCast( handle );
198   if ( control )
199   {
200     Internal::Control& controlImpl( Internal::GetImplementation( control ) );
201     connected = true;
202
203     if ( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
204     {
205       controlImpl.KeyEventSignal().Connect( tracker, functor );
206     }
207     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_GAINED ) )
208     {
209       controlImpl.KeyInputFocusGainedSignal().Connect( tracker, functor );
210     }
211     else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_LOST ) )
212     {
213       controlImpl.KeyInputFocusLostSignal().Connect( tracker, functor );
214     }
215     else if( 0 == strcmp( signalName.c_str(), SIGNAL_TAPPED ) )
216     {
217       controlImpl.EnableGestureDetection( Gesture::Tap );
218       controlImpl.GetTapGestureDetector().DetectedSignal().Connect( tracker, functor );
219     }
220     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PANNED ) )
221     {
222       controlImpl.EnableGestureDetection( Gesture::Pan );
223       controlImpl.GetPanGestureDetector().DetectedSignal().Connect( tracker, functor );
224     }
225     else if( 0 == strcmp( signalName.c_str(), SIGNAL_PINCHED ) )
226     {
227       controlImpl.EnableGestureDetection( Gesture::Pinch );
228       controlImpl.GetPinchGestureDetector().DetectedSignal().Connect( tracker, functor );
229     }
230     else if( 0 == strcmp( signalName.c_str(), SIGNAL_LONG_PRESSED ) )
231     {
232       controlImpl.EnableGestureDetection( Gesture::LongPress );
233       controlImpl.GetLongPressGestureDetector().DetectedSignal().Connect( tracker, functor );
234     }
235   }
236   return connected;
237 }
238
239 /**
240  * Creates control through type registry
241  */
242 BaseHandle Create()
243 {
244   return Internal::Control::New();
245 }
246 // Setup signals and actions using the type-registry.
247 DALI_TYPE_REGISTRATION_BEGIN( Control, CustomActor, Create );
248
249 // Note: Properties are registered separately below.
250
251 SignalConnectorType registerSignal1( typeRegistration, SIGNAL_KEY_EVENT, &DoConnectSignal );
252 SignalConnectorType registerSignal2( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_GAINED, &DoConnectSignal );
253 SignalConnectorType registerSignal3( typeRegistration, SIGNAL_KEY_INPUT_FOCUS_LOST, &DoConnectSignal );
254 SignalConnectorType registerSignal4( typeRegistration, SIGNAL_TAPPED, &DoConnectSignal );
255 SignalConnectorType registerSignal5( typeRegistration, SIGNAL_PANNED, &DoConnectSignal );
256 SignalConnectorType registerSignal6( typeRegistration, SIGNAL_PINCHED, &DoConnectSignal );
257 SignalConnectorType registerSignal7( typeRegistration, SIGNAL_LONG_PRESSED, &DoConnectSignal );
258
259 TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &DoAction );
260
261 DALI_TYPE_REGISTRATION_END()
262
263 } // unnamed namespace
264
265
266 // Properties registered without macro to use specific member variables.
267 const PropertyRegistration Control::Impl::PROPERTY_1( typeRegistration, "styleName",              Toolkit::Control::Property::STYLE_NAME,                   Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
268 const PropertyRegistration Control::Impl::PROPERTY_2( typeRegistration, "backgroundColor",        Toolkit::Control::Property::BACKGROUND_COLOR,             Property::VECTOR4, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
269 const PropertyRegistration Control::Impl::PROPERTY_3( typeRegistration, "backgroundImage",        Toolkit::Control::Property::BACKGROUND_IMAGE,             Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
270 const PropertyRegistration Control::Impl::PROPERTY_4( typeRegistration, "keyInputFocus",          Toolkit::Control::Property::KEY_INPUT_FOCUS,              Property::BOOLEAN, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
271 const PropertyRegistration Control::Impl::PROPERTY_5( typeRegistration, "background",             Toolkit::Control::Property::BACKGROUND,                   Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
272 const PropertyRegistration Control::Impl::PROPERTY_6( typeRegistration, "tooltip",                Toolkit::DevelControl::Property::TOOLTIP,                 Property::MAP,     &Control::Impl::SetProperty, &Control::Impl::GetProperty );
273 const PropertyRegistration Control::Impl::PROPERTY_7( typeRegistration, "state",                  Toolkit::DevelControl::Property::STATE,                   Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
274 const PropertyRegistration Control::Impl::PROPERTY_8( typeRegistration, "subState",               Toolkit::DevelControl::Property::SUB_STATE,               Property::STRING,  &Control::Impl::SetProperty, &Control::Impl::GetProperty );
275 const PropertyRegistration Control::Impl::PROPERTY_9( typeRegistration, "leftFocusableActorId",   Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
276 const PropertyRegistration Control::Impl::PROPERTY_10( typeRegistration, "rightFocusableActorId", Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID,Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
277 const PropertyRegistration Control::Impl::PROPERTY_11( typeRegistration, "upFocusableActorId",    Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID,   Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
278 const PropertyRegistration Control::Impl::PROPERTY_12( typeRegistration, "downFocusableActorId",  Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::INTEGER, &Control::Impl::SetProperty, &Control::Impl::GetProperty );
279
280
281
282 Control::Impl::Impl( Control& controlImpl )
283 : mControlImpl( controlImpl ),
284   mState( Toolkit::DevelControl::NORMAL ),
285   mSubStateName(""),
286   mLeftFocusableActorId( -1 ),
287   mRightFocusableActorId( -1 ),
288   mUpFocusableActorId( -1 ),
289   mDownFocusableActorId( -1 ),
290   mStyleName(""),
291   mBackgroundColor(Color::TRANSPARENT),
292   mStartingPinchScale( NULL ),
293   mKeyEventSignal(),
294   mPinchGestureDetector(),
295   mPanGestureDetector(),
296   mTapGestureDetector(),
297   mLongPressGestureDetector(),
298   mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
299   mIsKeyboardNavigationSupported( false ),
300   mIsKeyboardFocusGroup( false )
301 {
302
303 }
304
305 Control::Impl::~Impl()
306 {
307   // All gesture detectors will be destroyed so no need to disconnect.
308   delete mStartingPinchScale;
309 }
310
311 Control::Impl& Control::Impl::Get( Internal::Control& internalControl )
312 {
313   return *internalControl.mImpl;
314 }
315
316 const Control::Impl& Control::Impl::Get( const Internal::Control& internalControl )
317 {
318   return *internalControl.mImpl;
319 }
320
321 // Gesture Detection Methods
322 void Control::Impl::PinchDetected(Actor actor, const PinchGesture& pinch)
323 {
324   mControlImpl.OnPinch(pinch);
325 }
326
327 void Control::Impl::PanDetected(Actor actor, const PanGesture& pan)
328 {
329   mControlImpl.OnPan(pan);
330 }
331
332 void Control::Impl::TapDetected(Actor actor, const TapGesture& tap)
333 {
334   mControlImpl.OnTap(tap);
335 }
336
337 void Control::Impl::LongPressDetected(Actor actor, const LongPressGesture& longPress)
338 {
339   mControlImpl.OnLongPress(longPress);
340 }
341
342 // Called by a Visual when it's resource is ready
343 void Control::Impl::ResourceReady( Visual::Base& object)
344 {
345
346   // go through and check if all the visuals are ready, if they are emit a signal
347   for ( RegisteredVisualContainer::ConstIterator visualIter = mVisuals.Begin();
348         visualIter != mVisuals.End(); ++visualIter )
349   {
350     const Toolkit::Visual::Base visual = (*visualIter)->visual;
351     const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
352
353     // one of the visuals is not ready
354     if( !visualImpl.IsResourceReady() )
355     {
356       return;
357     }
358   }
359
360   // all the visuals are ready
361   Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
362   mResourceReadySignal.Emit( handle );
363
364 }
365
366 bool Control::Impl::IsResourceReady() const
367 {
368   // go through and check all the visuals are ready
369   for ( RegisteredVisualContainer::ConstIterator visualIter = mVisuals.Begin();
370          visualIter != mVisuals.End(); ++visualIter )
371    {
372      const Toolkit::Visual::Base visual = (*visualIter)->visual;
373      const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
374
375      // one of the visuals is not ready
376      if( !visualImpl.IsResourceReady()  )
377      {
378        return false;
379      }
380    }
381   return true;
382 }
383
384 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
385 {
386   RegisterVisual( index, visual, true );
387 }
388
389 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
390 {
391   bool visualReplaced ( false );
392   Actor self = mControlImpl.Self();
393
394   if( !mVisuals.Empty() )
395   {
396     RegisteredVisualContainer::Iterator iter;
397     // Check if visual (index) is already registered.  Replace if so.
398     if ( FindVisual( index, mVisuals, iter ) )
399     {
400       if( (*iter)->visual && self.OnStage() )
401       {
402         Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
403       }
404
405       StopObservingVisual( (*iter)->visual );
406       StartObservingVisual( visual );
407
408       (*iter)->visual = visual;
409       visualReplaced = true;
410     }
411   }
412
413   // If not set, set the name of the visual to the same name as the control's property.
414   // ( If the control has been type registered )
415   if( visual.GetName().empty() )
416   {
417     // Check if the control has been type registered:
418     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid( mControlImpl ) );
419     if( typeInfo )
420     {
421       // Check if the property index has been registered:
422       Property::IndexContainer indices;
423       typeInfo.GetPropertyIndices( indices );
424       Property::IndexContainer::Iterator iter = std::find( indices.Begin(), indices.End(), index );
425       if( iter != indices.End() )
426       {
427         // If it has, then get it's name and use that for the visual
428         std::string visualName = typeInfo.GetPropertyName( index );
429         visual.SetName( visualName );
430       }
431     }
432   }
433
434   if( !visualReplaced ) // New registration entry
435   {
436     mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
437
438     // monitor when the visuals resources are ready
439     StartObservingVisual( visual );
440
441   }
442
443   if( visual && self.OnStage() && enabled )
444   {
445     Toolkit::GetImplementation(visual).SetOnStage( self );
446   }
447
448   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n",  visual.GetName().c_str(), index, enabled?"T":"F" );
449 }
450
451 void Control::Impl::UnregisterVisual( Property::Index index )
452 {
453    RegisteredVisualContainer::Iterator iter;
454    if ( FindVisual( index, mVisuals, iter ) )
455    {
456      // stop observing visual
457      StopObservingVisual( (*iter)->visual );
458
459      Actor self( mControlImpl.Self() );
460      Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
461      (*iter)->visual.Reset();
462      mVisuals.Erase( iter );
463    }
464 }
465
466 Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
467 {
468   RegisteredVisualContainer::Iterator iter;
469   if ( FindVisual( index, mVisuals, iter ) )
470   {
471     return (*iter)->visual;
472   }
473
474   return Toolkit::Visual::Base();
475 }
476
477 void Control::Impl::EnableVisual( Property::Index index, bool enable )
478 {
479   RegisteredVisualContainer::Iterator iter;
480   if ( FindVisual( index, mVisuals, iter ) )
481   {
482     if (  (*iter)->enabled == enable )
483     {
484       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
485       return;
486     }
487
488     (*iter)->enabled = enable;
489     Actor parentActor = mControlImpl.Self();
490     if ( mControlImpl.Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
491     {
492       if ( enable )
493       {
494         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
495         Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
496       }
497       else
498       {
499         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
500         Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor );  // No need to call if control not staged.
501       }
502     }
503   }
504 }
505
506 bool Control::Impl::IsVisualEnabled( Property::Index index ) const
507 {
508   RegisteredVisualContainer::Iterator iter;
509   if ( FindVisual( index, mVisuals, iter ) )
510   {
511     return (*iter)->enabled;
512   }
513   return false;
514 }
515
516 void Control::Impl::StopObservingVisual( Toolkit::Visual::Base& visual )
517 {
518   Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
519
520   // Stop observing the visual
521   visualImpl.RemoveResourceObserver( *this );
522 }
523
524 void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
525 {
526   Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
527
528   // start observing the visual for resource ready
529   visualImpl.AddResourceObserver( *this );
530 }
531
532 Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
533 {
534   Dali::Animation transition;
535   const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
536
537   if( transitionData.Count() > 0 )
538   {
539     // Setup a Transition from TransitionData.
540     TransitionData::Iterator end = transitionData.End();
541     for( TransitionData::Iterator iter = transitionData.Begin() ;
542          iter != end; ++iter )
543     {
544       TransitionData::Animator* animator = (*iter);
545
546       Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
547
548       if( visual )
549       {
550         Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
551         visualImpl.AnimateProperty( transition, *animator );
552       }
553       else
554       {
555         // Otherwise, try any actor children of control (Including the control)
556         Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
557         if( child )
558         {
559           Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
560           if( propertyIndex != Property::INVALID_INDEX )
561           {
562             if( animator->animate == false )
563             {
564               if( animator->targetValue.GetType() != Property::NONE )
565               {
566                 child.SetProperty( propertyIndex, animator->targetValue );
567               }
568             }
569             else // animate the property
570             {
571               if( animator->initialValue.GetType() != Property::NONE )
572               {
573                 child.SetProperty( propertyIndex, animator->initialValue );
574               }
575
576               if( ! transition )
577               {
578                 transition = Dali::Animation::New( 0.1f );
579               }
580
581               transition.AnimateTo( Property( child, propertyIndex ),
582                                     animator->targetValue,
583                                     animator->alphaFunction,
584                                     TimePeriod( animator->timePeriodDelay,
585                                                 animator->timePeriodDuration ) );
586             }
587           }
588         }
589       }
590     }
591   }
592
593   return transition;
594 }
595
596 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
597 {
598   Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
599
600   if ( control )
601   {
602     Control& controlImpl( GetImplementation( control ) );
603
604     switch ( index )
605     {
606       case Toolkit::Control::Property::STYLE_NAME:
607       {
608         controlImpl.SetStyleName( value.Get< std::string >() );
609         break;
610       }
611
612       case Toolkit::DevelControl::Property::STATE:
613       {
614         bool withTransitions=true;
615         const Property::Value* valuePtr=&value;
616         Property::Map* map = value.GetMap();
617         if(map)
618         {
619           Property::Value* value2 = map->Find("withTransitions");
620           if( value2 )
621           {
622             withTransitions = value2->Get<bool>();
623           }
624
625           valuePtr = map->Find("state");
626         }
627
628         if( valuePtr )
629         {
630           Toolkit::DevelControl::State state( controlImpl.mImpl->mState );
631           if( Scripting::GetEnumerationProperty< Toolkit::DevelControl::State >( *valuePtr, ControlStateTable, ControlStateTableCount, state ) )
632           {
633             controlImpl.mImpl->SetState( state, withTransitions );
634           }
635         }
636       }
637       break;
638
639       case Toolkit::DevelControl::Property::SUB_STATE:
640       {
641         std::string subState;
642         if( value.Get( subState ) )
643         {
644           controlImpl.mImpl->SetSubState( subState );
645         }
646       }
647       break;
648
649       case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
650       {
651         int focusId;
652         if( value.Get( focusId ) )
653         {
654           controlImpl.mImpl->mLeftFocusableActorId = focusId;
655         }
656       }
657       break;
658
659       case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
660       {
661         int focusId;
662         if( value.Get( focusId ) )
663         {
664           controlImpl.mImpl->mRightFocusableActorId = focusId;
665         }
666       }
667       break;
668
669       case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
670       {
671         int focusId;
672         if( value.Get( focusId ) )
673         {
674           controlImpl.mImpl->mUpFocusableActorId = focusId;
675         }
676       }
677       break;
678
679       case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
680       {
681         int focusId;
682         if( value.Get( focusId ) )
683         {
684           controlImpl.mImpl->mDownFocusableActorId = focusId;
685         }
686       }
687       break;
688
689       case Toolkit::Control::Property::BACKGROUND_COLOR:
690       {
691         DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
692         controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
693         break;
694       }
695
696       case Toolkit::Control::Property::BACKGROUND_IMAGE:
697       {
698         DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
699         Image image = Scripting::NewImage( value );
700         if ( image )
701         {
702           controlImpl.SetBackgroundImage( image );
703         }
704         else
705         {
706           // An empty image means the background is no longer required
707           controlImpl.ClearBackground();
708         }
709         break;
710       }
711
712       case Toolkit::Control::Property::KEY_INPUT_FOCUS:
713       {
714         if ( value.Get< bool >() )
715         {
716           controlImpl.SetKeyInputFocus();
717         }
718         else
719         {
720           controlImpl.ClearKeyInputFocus();
721         }
722         break;
723       }
724
725       case Toolkit::Control::Property::BACKGROUND:
726       {
727         std::string url;
728         Vector4 color;
729         const Property::Map* map = value.GetMap();
730         if( map && !map->Empty() )
731         {
732           controlImpl.SetBackground( *map );
733         }
734         else if( value.Get( url ) )
735         {
736           // don't know the size to load
737           Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
738           if( visual )
739           {
740             controlImpl.mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
741             visual.SetDepthIndex( DepthIndex::BACKGROUND );
742           }
743         }
744         else if( value.Get( color ) )
745         {
746           controlImpl.SetBackgroundColor(color);
747         }
748         else
749         {
750           // The background is an empty property map, so we should clear the background
751           controlImpl.ClearBackground();
752         }
753         break;
754       }
755
756       case Toolkit::DevelControl::Property::TOOLTIP:
757       {
758         TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
759         if( ! tooltipPtr )
760         {
761           tooltipPtr = Tooltip::New( control );
762         }
763         tooltipPtr->SetProperties( value );
764       }
765     }
766   }
767 }
768
769 Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index index )
770 {
771   Property::Value value;
772
773   Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
774
775   if ( control )
776   {
777     Control& controlImpl( GetImplementation( control ) );
778
779     switch ( index )
780     {
781       case Toolkit::Control::Property::STYLE_NAME:
782       {
783         value = controlImpl.GetStyleName();
784         break;
785       }
786
787       case Toolkit::DevelControl::Property::STATE:
788       {
789         value = controlImpl.mImpl->mState;
790         break;
791       }
792
793       case Toolkit::DevelControl::Property::SUB_STATE:
794       {
795         value = controlImpl.mImpl->mSubStateName;
796         break;
797       }
798
799       case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
800       {
801         value = controlImpl.mImpl->mLeftFocusableActorId;
802         break;
803       }
804
805       case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
806       {
807         value = controlImpl.mImpl->mRightFocusableActorId;
808         break;
809       }
810
811       case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
812       {
813         value = controlImpl.mImpl->mUpFocusableActorId;
814         break;
815       }
816
817       case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
818       {
819         value = controlImpl.mImpl->mDownFocusableActorId;
820         break;
821       }
822
823       case Toolkit::Control::Property::BACKGROUND_COLOR:
824       {
825         DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
826         value = controlImpl.GetBackgroundColor();
827         break;
828       }
829
830       case Toolkit::Control::Property::BACKGROUND_IMAGE:
831       {
832         DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
833         Property::Map map;
834         Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
835         if( visual )
836         {
837           visual.CreatePropertyMap( map );
838         }
839         value = map;
840         break;
841       }
842
843       case Toolkit::Control::Property::KEY_INPUT_FOCUS:
844       {
845         value = controlImpl.HasKeyInputFocus();
846         break;
847       }
848
849       case Toolkit::Control::Property::BACKGROUND:
850       {
851         Property::Map map;
852         Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
853         if( visual )
854         {
855           visual.CreatePropertyMap( map );
856         }
857
858         value = map;
859         break;
860       }
861
862       case Toolkit::DevelControl::Property::TOOLTIP:
863       {
864         Property::Map map;
865         if( controlImpl.mImpl->mTooltip )
866         {
867           controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
868         }
869         value = map;
870         break;
871       }
872
873     }
874   }
875
876   return value;
877 }
878
879
880 void  Control::Impl::CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties )
881 {
882   for(RegisteredVisualContainer::Iterator iter = visuals.Begin(); iter!= visuals.End(); iter++)
883   {
884     if( (*iter)->visual )
885     {
886       Property::Map instanceMap;
887       Toolkit::GetImplementation((*iter)->visual).CreateInstancePropertyMap(instanceMap);
888       instancedProperties.Add( (*iter)->visual.GetName(), instanceMap );
889     }
890   }
891 }
892
893
894 void Control::Impl::RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName )
895 {
896   Actor self( mControlImpl.Self() );
897
898   for ( RegisteredVisualContainer::Iterator visualIter = visuals.Begin();
899         visualIter != visuals.End(); ++visualIter )
900   {
901     Toolkit::Visual::Base visual = (*visualIter)->visual;
902     if( visual && visual.GetName() == visualName )
903     {
904       Toolkit::GetImplementation(visual).SetOffStage( self );
905       (*visualIter)->visual.Reset();
906       visuals.Erase( visualIter );
907       break;
908     }
909   }
910 }
911
912 void Control::Impl::RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals )
913 {
914   Actor self( mControlImpl.Self() );
915   for( DictionaryKeys::iterator iter = removeVisuals.begin(); iter != removeVisuals.end(); ++iter )
916   {
917     const std::string visualName = *iter;
918     RemoveVisual( visuals, visualName );
919   }
920 }
921
922 void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange,
923                              Dictionary<Property::Map>& instancedProperties )
924 {
925   Dali::CustomActor handle( mControlImpl.GetOwner() );
926   for( Dictionary<Property::Map>::iterator iter = stateVisualsToChange.Begin();
927        iter != stateVisualsToChange.End(); ++iter )
928   {
929     const std::string& visualName = (*iter).key;
930     const Property::Map& toMap = (*iter).entry;
931
932     // is it a candidate for re-creation?
933     bool recreate = false;
934
935     Toolkit::Visual::Base visual = GetVisualByName( mVisuals, visualName );
936     if( visual )
937     {
938       Property::Map fromMap;
939       visual.CreatePropertyMap( fromMap );
940
941       Toolkit::DevelVisual::Type fromType = GetVisualTypeFromMap( fromMap );
942       Toolkit::DevelVisual::Type toType = GetVisualTypeFromMap( toMap );
943
944       if( fromType != toType )
945       {
946         recreate = true;
947       }
948       else
949       {
950         if( fromType == Toolkit::DevelVisual::IMAGE || fromType == Toolkit::DevelVisual::N_PATCH
951             || fromType == Toolkit::DevelVisual::SVG || fromType == Toolkit::DevelVisual::ANIMATED_IMAGE )
952         {
953           Property::Value* fromUrl = fromMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
954           Property::Value* toUrl = toMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
955
956           if( fromUrl && toUrl )
957           {
958             std::string fromUrlString;
959             std::string toUrlString;
960             fromUrl->Get(fromUrlString);
961             toUrl->Get(toUrlString);
962
963             if( fromUrlString != toUrlString )
964             {
965               recreate = true;
966             }
967           }
968         }
969       }
970
971       const Property::Map* instancedMap = instancedProperties.FindConst( visualName );
972       if( recreate || instancedMap )
973       {
974         RemoveVisual( mVisuals, visualName );
975         Style::ApplyVisual( handle, visualName, toMap, instancedMap );
976       }
977       else
978       {
979         // @todo check to see if we can apply toMap without recreating the visual
980         // e.g. by setting only animatable properties
981         // For now, recreate all visuals, but merge in instance data.
982         RemoveVisual( mVisuals, visualName );
983         Style::ApplyVisual( handle, visualName, toMap, instancedMap );
984       }
985     }
986   }
987 }
988
989 void Control::Impl::ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState )
990 {
991   // Collect all old visual names
992   DictionaryKeys stateVisualsToRemove;
993   if( oldState )
994   {
995     oldState->visuals.GetKeys( stateVisualsToRemove );
996     if( ! subState.empty() )
997     {
998       const StylePtr* oldSubState = oldState->subStates.FindConst(subState);
999       if( oldSubState )
1000       {
1001         DictionaryKeys subStateVisualsToRemove;
1002         (*oldSubState)->visuals.GetKeys( subStateVisualsToRemove );
1003         Merge( stateVisualsToRemove, subStateVisualsToRemove );
1004       }
1005     }
1006   }
1007
1008   // Collect all new visual properties
1009   Dictionary<Property::Map> stateVisualsToAdd;
1010   if( newState )
1011   {
1012     stateVisualsToAdd = newState->visuals;
1013     if( ! subState.empty() )
1014     {
1015       const StylePtr* newSubState = newState->subStates.FindConst(subState);
1016       if( newSubState )
1017       {
1018         stateVisualsToAdd.Merge( (*newSubState)->visuals );
1019       }
1020     }
1021   }
1022
1023   // If a name is in both add/remove, move it to change list.
1024   Dictionary<Property::Map> stateVisualsToChange;
1025   FindChangableVisuals( stateVisualsToAdd, stateVisualsToChange, stateVisualsToRemove);
1026
1027   // Copy instanced properties (e.g. text label) of current visuals
1028   Dictionary<Property::Map> instancedProperties;
1029   CopyInstancedProperties( mVisuals, instancedProperties );
1030
1031   // For each visual in remove list, remove from mVisuals
1032   RemoveVisuals( mVisuals, stateVisualsToRemove );
1033
1034   // For each visual in add list, create and add to mVisuals
1035   Dali::CustomActor handle( mControlImpl.GetOwner() );
1036   Style::ApplyVisuals( handle, stateVisualsToAdd, instancedProperties );
1037
1038   // For each visual in change list, if it requires a new visual,
1039   // remove old visual, create and add to mVisuals
1040   RecreateChangedVisuals( stateVisualsToChange, instancedProperties );
1041 }
1042
1043 void Control::Impl::SetState( DevelControl::State newState, bool withTransitions )
1044 {
1045   DevelControl::State oldState = mState;
1046   Dali::CustomActor handle( mControlImpl.GetOwner() );
1047   DALI_LOG_INFO(gLogFilter, Debug::Concise, "Control::Impl::SetState: %s\n",
1048                 (mState == DevelControl::NORMAL ? "NORMAL" :(
1049                   mState == DevelControl::FOCUSED ?"FOCUSED" : (
1050                     mState == DevelControl::DISABLED?"DISABLED":"NONE" ))));
1051
1052   if( mState != newState )
1053   {
1054     // If mState was Disabled, and new state is Focused, should probably
1055     // store that fact, e.g. in another property that FocusManager can access.
1056     mState = newState;
1057
1058     // Trigger state change and transitions
1059     // Apply new style, if stylemanager is available
1060     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1061     if( styleManager )
1062     {
1063       const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1064
1065       if( stylePtr )
1066       {
1067         std::string oldStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( oldState, ControlStateTable, ControlStateTableCount );
1068         std::string newStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( newState, ControlStateTable, ControlStateTableCount );
1069
1070         const StylePtr* newStateStyle = stylePtr->subStates.Find( newStateName );
1071         const StylePtr* oldStateStyle = stylePtr->subStates.Find( oldStateName );
1072         if( oldStateStyle && newStateStyle )
1073         {
1074           // Only change if both state styles exist
1075           ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, mSubStateName );
1076         }
1077       }
1078     }
1079   }
1080 }
1081
1082 void Control::Impl::SetSubState( const std::string& subStateName, bool withTransitions )
1083 {
1084   if( mSubStateName != subStateName )
1085   {
1086     // Get existing sub-state visuals, and unregister them
1087     Dali::CustomActor handle( mControlImpl.GetOwner() );
1088
1089     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1090     if( styleManager )
1091     {
1092       const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1093       if( stylePtr )
1094       {
1095         // Stringify state
1096         std::string stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( mState, ControlStateTable, ControlStateTableCount );
1097
1098         const StylePtr* state = stylePtr->subStates.Find( stateName );
1099         if( state )
1100         {
1101           StylePtr stateStyle(*state);
1102
1103           const StylePtr* newStateStyle = stateStyle->subStates.Find( subStateName );
1104           const StylePtr* oldStateStyle = stateStyle->subStates.Find( mSubStateName );
1105           if( oldStateStyle && newStateStyle )
1106           {
1107             std::string empty;
1108             ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, empty );
1109           }
1110         }
1111       }
1112     }
1113
1114     mSubStateName = subStateName;
1115   }
1116 }
1117
1118 } // namespace Internal
1119
1120 } // namespace Toolkit
1121
1122 } // namespace Dali