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