a5e4b988857cb1a9fd744f0bafb44b1520eeec10
[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   // Emit signal if all enabled visuals registered by the control are ready.
404   // If any visual is registered and enabled but not ready then the control will not be able to emit ReadySignal.
405
406   if ( IsResourceReady() )
407   {
408     Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
409     mResourceReadySignal.Emit( handle );
410   }
411 }
412
413 bool Control::Impl::IsResourceReady() const
414 {
415   // Iterate through and check all the enabled visuals are ready
416   for ( auto visualIter = mVisuals.Begin();
417          visualIter != mVisuals.End(); ++visualIter )
418   {
419     const Toolkit::Visual::Base visual = (*visualIter)->visual;
420     const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
421
422     // one of the enabled visuals is not ready
423     if( !visualImpl.IsResourceReady() && (*visualIter)->enabled )
424     {
425       return false;
426     }
427   }
428   return true;
429 }
430
431 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
432 {
433   RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::NOT_SET );
434 }
435
436 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex )
437 {
438   RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::SET, depthIndex );
439 }
440
441 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
442 {
443   RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::NOT_SET );
444 }
445
446 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex )
447 {
448   RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::SET, depthIndex );
449 }
450
451 void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex )
452 {
453   DALI_LOG_INFO( gLogFilter, Debug::Concise, "RegisterVisual:%d \n", index );
454
455   bool visualReplaced ( false );
456   Actor self = mControlImpl.Self();
457
458   if( !mVisuals.Empty() )
459   {
460     RegisteredVisualContainer::Iterator registeredVisualsiter;
461     // Check if visual (index) is already registered.  Replace if so.
462     if( FindVisual( index, mVisuals, registeredVisualsiter ) )
463     {
464       if( (*registeredVisualsiter)->visual )
465       {
466         // Store current visual depth index as may need to set the replacement visual to same depth
467         const int currentDepthIndex = (*registeredVisualsiter)->visual.GetDepthIndex();
468
469         // Monitor when the visuals resources are ready
470         StopObservingVisual( (*registeredVisualsiter)->visual );
471         StartObservingVisual( visual );
472
473         if(  self.OnStage() )
474         {
475           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Adding visual to replacement Queue: %d \n", index );
476           // Check if visual is currently in the process of being replaced
477           RegisteredVisualContainer::Iterator queuedReplacementVisual;
478           if ( FindVisual( index, mReplacementVisuals, queuedReplacementVisual ) )
479           {
480             // If visual on replacement queue is going to be replaced before it's ready then will be removed from queue (and stage)
481             // Only the the last requested visual will be queued and then displayed.
482             Toolkit::GetImplementation( (*queuedReplacementVisual)->visual ).SetOffStage( self );
483             mReplacementVisuals.Erase(queuedReplacementVisual);
484           }
485           // Add to replacement list
486           mReplacementVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
487         }
488         else
489         {
490           // Not staged so can just replace registered visual
491           (*registeredVisualsiter)->visual = visual;
492           (*registeredVisualsiter)->enabled = ( enabled == VisualState::ENABLED ) ? true : false;
493         }
494
495         // 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
496         if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
497             ( visual.GetDepthIndex() == 0 ) )
498         {
499           visual.SetDepthIndex( currentDepthIndex );
500         }
501       }
502
503       visualReplaced = true;
504     }
505   }
506
507   // If not set, set the name of the visual to the same name as the control's property.
508   // ( If the control has been type registered )
509   if( visual.GetName().empty() )
510   {
511     try
512     {
513       std::string visualName = self.GetPropertyName( index );
514       if( !visualName.empty() )
515       {
516         DALI_LOG_INFO( gLogFilter, Debug::Concise, "Setting visual name for property %d to %s\n",
517                        index, visualName.c_str() );
518         visual.SetName( visualName );
519       }
520     }
521     catch( Dali::DaliException e )
522     {
523       DALI_LOG_WARNING( "Attempting to register visual without a registered property, index: %d\n", index );
524     }
525   }
526
527   if( !visualReplaced ) // New registration entry
528   {
529     DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration %d\n", index);
530     mVisuals.PushBack( new RegisteredVisual( index, visual, ( enabled == VisualState::ENABLED ? true : false ) ) );
531
532     // monitor when the visuals resources are ready
533     StartObservingVisual( visual );
534
535     // 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
536     if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
537         ( mVisuals.Size() > 1 ) &&
538         ( visual.GetDepthIndex() == 0 ) )
539     {
540       int maxDepthIndex = std::numeric_limits< int >::min();
541
542       RegisteredVisualContainer::ConstIterator iter;
543       const RegisteredVisualContainer::ConstIterator endIter = mVisuals.End();
544       for ( iter = mVisuals.Begin(); iter != endIter; iter++ )
545       {
546         const int visualDepthIndex = (*iter)->visual.GetDepthIndex();
547         if ( visualDepthIndex > maxDepthIndex )
548         {
549           maxDepthIndex = visualDepthIndex;
550         }
551       }
552
553       ++maxDepthIndex; // Add one to the current maximum depth index so that our added visual appears on top
554       visual.SetDepthIndex( maxDepthIndex );
555     }
556   }
557
558   if( visual )
559   {
560     // If the caller has set the depth-index, then set it here
561     if( depthIndexValueSet == DepthIndexValue::SET )
562     {
563       visual.SetDepthIndex( depthIndex );
564     }
565
566     // Put on stage if enabled and the control is already on the stage
567     if( ( enabled == VisualState::ENABLED ) && self.OnStage() )
568     {
569       // Visual must be set on stage for the renderer to be created and the ResourceReady triggered.
570       Toolkit::GetImplementation(visual).SetOnStage( self );
571     }
572   }
573
574   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n",  visual.GetName().c_str(), index, enabled?"T":"F" );
575 }
576
577 void Control::Impl::UnregisterVisual( Property::Index index )
578 {
579    RegisteredVisualContainer::Iterator iter;
580    if ( FindVisual( index, mVisuals, iter ) )
581    {
582      // stop observing visual
583      StopObservingVisual( (*iter)->visual );
584
585      Actor self( mControlImpl.Self() );
586      Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
587      (*iter)->visual.Reset();
588      mVisuals.Erase( iter );
589    }
590 }
591
592 Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
593 {
594   RegisteredVisualContainer::Iterator iter;
595   if ( FindVisual( index, mVisuals, iter ) )
596   {
597     return (*iter)->visual;
598   }
599
600   return Toolkit::Visual::Base();
601 }
602
603 void Control::Impl::EnableVisual( Property::Index index, bool enable )
604 {
605   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual (%d)\n", index);
606
607   RegisteredVisualContainer::Iterator iter;
608   if ( FindVisual( index, mVisuals, iter ) )
609   {
610     if (  (*iter)->enabled == enable )
611     {
612       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
613       return;
614     }
615
616     (*iter)->enabled = enable;
617     Actor parentActor = mControlImpl.Self();
618     if ( mControlImpl.Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
619     {
620       if ( enable )
621       {
622         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
623         Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
624       }
625       else
626       {
627         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
628         Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor );  // No need to call if control not staged.
629       }
630     }
631   }
632 }
633
634 bool Control::Impl::IsVisualEnabled( Property::Index index ) const
635 {
636   RegisteredVisualContainer::Iterator iter;
637   if ( FindVisual( index, mVisuals, iter ) )
638   {
639     return (*iter)->enabled;
640   }
641   return false;
642 }
643
644 void Control::Impl::StopObservingVisual( Toolkit::Visual::Base& visual )
645 {
646   Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
647
648   // Stop observing the visual
649   visualImpl.RemoveResourceObserver( *this );
650 }
651
652 void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
653 {
654   Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
655
656   // start observing the visual for resource ready
657   visualImpl.AddResourceObserver( *this );
658 }
659
660 Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
661 {
662   Dali::Animation transition;
663   const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
664
665   if( transitionData.Count() > 0 )
666   {
667     // Setup a Transition from TransitionData.
668     TransitionData::Iterator end = transitionData.End();
669     for( TransitionData::Iterator iter = transitionData.Begin() ;
670          iter != end; ++iter )
671     {
672       TransitionData::Animator* animator = (*iter);
673
674       Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
675
676       if( visual )
677       {
678 #if defined(DEBUG_ENABLED)
679         Dali::TypeInfo typeInfo;
680         ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
681         if( controlWrapperImpl )
682         {
683           typeInfo = controlWrapperImpl->GetTypeInfo();
684         }
685
686         DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
687                        visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
688 #endif
689         Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
690         visualImpl.AnimateProperty( transition, *animator );
691       }
692       else
693       {
694         DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
695         // Otherwise, try any actor children of control (Including the control)
696         Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
697         if( child )
698         {
699           Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
700           if( propertyIndex != Property::INVALID_INDEX )
701           {
702             if( animator->animate == false )
703             {
704               if( animator->targetValue.GetType() != Property::NONE )
705               {
706                 child.SetProperty( propertyIndex, animator->targetValue );
707               }
708             }
709             else // animate the property
710             {
711               if( animator->initialValue.GetType() != Property::NONE )
712               {
713                 child.SetProperty( propertyIndex, animator->initialValue );
714               }
715
716               if( ! transition )
717               {
718                 transition = Dali::Animation::New( 0.1f );
719               }
720
721               transition.AnimateTo( Property( child, propertyIndex ),
722                                     animator->targetValue,
723                                     animator->alphaFunction,
724                                     TimePeriod( animator->timePeriodDelay,
725                                                 animator->timePeriodDuration ) );
726             }
727           }
728         }
729       }
730     }
731   }
732
733   return transition;
734 }
735
736 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
737 {
738   Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
739
740   if ( control )
741   {
742     Control& controlImpl( GetImplementation( control ) );
743
744     switch ( index )
745     {
746       case Toolkit::Control::Property::STYLE_NAME:
747       {
748         controlImpl.SetStyleName( value.Get< std::string >() );
749         break;
750       }
751
752       case Toolkit::DevelControl::Property::STATE:
753       {
754         bool withTransitions=true;
755         const Property::Value* valuePtr=&value;
756         Property::Map* map = value.GetMap();
757         if(map)
758         {
759           Property::Value* value2 = map->Find("withTransitions");
760           if( value2 )
761           {
762             withTransitions = value2->Get<bool>();
763           }
764
765           valuePtr = map->Find("state");
766         }
767
768         if( valuePtr )
769         {
770           Toolkit::DevelControl::State state( controlImpl.mImpl->mState );
771           if( Scripting::GetEnumerationProperty< Toolkit::DevelControl::State >( *valuePtr, ControlStateTable, ControlStateTableCount, state ) )
772           {
773             controlImpl.mImpl->SetState( state, withTransitions );
774           }
775         }
776       }
777       break;
778
779       case Toolkit::DevelControl::Property::SUB_STATE:
780       {
781         std::string subState;
782         if( value.Get( subState ) )
783         {
784           controlImpl.mImpl->SetSubState( subState );
785         }
786       }
787       break;
788
789       case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
790       {
791         int focusId;
792         if( value.Get( focusId ) )
793         {
794           controlImpl.mImpl->mLeftFocusableActorId = focusId;
795         }
796       }
797       break;
798
799       case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
800       {
801         int focusId;
802         if( value.Get( focusId ) )
803         {
804           controlImpl.mImpl->mRightFocusableActorId = focusId;
805         }
806       }
807       break;
808
809       case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
810       {
811         int focusId;
812         if( value.Get( focusId ) )
813         {
814           controlImpl.mImpl->mUpFocusableActorId = focusId;
815         }
816       }
817       break;
818
819       case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
820       {
821         int focusId;
822         if( value.Get( focusId ) )
823         {
824           controlImpl.mImpl->mDownFocusableActorId = focusId;
825         }
826       }
827       break;
828
829       case Toolkit::Control::Property::BACKGROUND_COLOR:
830       {
831         DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
832         controlImpl.SetBackgroundColor( value.Get< Vector4 >() );
833         break;
834       }
835
836       case Toolkit::Control::Property::BACKGROUND_IMAGE:
837       {
838         DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
839         Image image = Scripting::NewImage( value );
840         if ( image )
841         {
842           controlImpl.SetBackgroundImage( image );
843         }
844         else
845         {
846           // An empty image means the background is no longer required
847           controlImpl.ClearBackground();
848         }
849         break;
850       }
851
852       case Toolkit::Control::Property::KEY_INPUT_FOCUS:
853       {
854         if ( value.Get< bool >() )
855         {
856           controlImpl.SetKeyInputFocus();
857         }
858         else
859         {
860           controlImpl.ClearKeyInputFocus();
861         }
862         break;
863       }
864
865       case Toolkit::Control::Property::BACKGROUND:
866       {
867         std::string url;
868         Vector4 color;
869         const Property::Map* map = value.GetMap();
870         if( map && !map->Empty() )
871         {
872           controlImpl.SetBackground( *map );
873         }
874         else if( value.Get( url ) )
875         {
876           // don't know the size to load
877           Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
878           if( visual )
879           {
880             controlImpl.mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
881           }
882         }
883         else if( value.Get( color ) )
884         {
885           controlImpl.SetBackgroundColor(color);
886         }
887         else
888         {
889           // The background is an empty property map, so we should clear the background
890           controlImpl.ClearBackground();
891         }
892         break;
893       }
894
895       case Toolkit::DevelControl::Property::TOOLTIP:
896       {
897         TooltipPtr& tooltipPtr = controlImpl.mImpl->mTooltip;
898         if( ! tooltipPtr )
899         {
900           tooltipPtr = Tooltip::New( control );
901         }
902         tooltipPtr->SetProperties( value );
903       }
904     }
905   }
906 }
907
908 Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index index )
909 {
910   Property::Value value;
911
912   Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
913
914   if ( control )
915   {
916     Control& controlImpl( GetImplementation( control ) );
917
918     switch ( index )
919     {
920       case Toolkit::Control::Property::STYLE_NAME:
921       {
922         value = controlImpl.GetStyleName();
923         break;
924       }
925
926       case Toolkit::DevelControl::Property::STATE:
927       {
928         value = controlImpl.mImpl->mState;
929         break;
930       }
931
932       case Toolkit::DevelControl::Property::SUB_STATE:
933       {
934         value = controlImpl.mImpl->mSubStateName;
935         break;
936       }
937
938       case Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID:
939       {
940         value = controlImpl.mImpl->mLeftFocusableActorId;
941         break;
942       }
943
944       case Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID:
945       {
946         value = controlImpl.mImpl->mRightFocusableActorId;
947         break;
948       }
949
950       case Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID:
951       {
952         value = controlImpl.mImpl->mUpFocusableActorId;
953         break;
954       }
955
956       case Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID:
957       {
958         value = controlImpl.mImpl->mDownFocusableActorId;
959         break;
960       }
961
962       case Toolkit::Control::Property::BACKGROUND_COLOR:
963       {
964         DALI_LOG_WARNING( "BACKGROUND_COLOR property is deprecated. Use BACKGROUND property instead\n" );
965         value = controlImpl.GetBackgroundColor();
966         break;
967       }
968
969       case Toolkit::Control::Property::BACKGROUND_IMAGE:
970       {
971         DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
972         Property::Map map;
973         Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
974         if( visual )
975         {
976           visual.CreatePropertyMap( map );
977         }
978         value = map;
979         break;
980       }
981
982       case Toolkit::Control::Property::KEY_INPUT_FOCUS:
983       {
984         value = controlImpl.HasKeyInputFocus();
985         break;
986       }
987
988       case Toolkit::Control::Property::BACKGROUND:
989       {
990         Property::Map map;
991         Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
992         if( visual )
993         {
994           visual.CreatePropertyMap( map );
995         }
996
997         value = map;
998         break;
999       }
1000
1001       case Toolkit::DevelControl::Property::TOOLTIP:
1002       {
1003         Property::Map map;
1004         if( controlImpl.mImpl->mTooltip )
1005         {
1006           controlImpl.mImpl->mTooltip->CreatePropertyMap( map );
1007         }
1008         value = map;
1009         break;
1010       }
1011
1012     }
1013   }
1014
1015   return value;
1016 }
1017
1018
1019 void  Control::Impl::CopyInstancedProperties( RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties )
1020 {
1021   for(RegisteredVisualContainer::Iterator iter = visuals.Begin(); iter!= visuals.End(); iter++)
1022   {
1023     if( (*iter)->visual )
1024     {
1025       Property::Map instanceMap;
1026       Toolkit::GetImplementation((*iter)->visual).CreateInstancePropertyMap(instanceMap);
1027       instancedProperties.Add( (*iter)->visual.GetName(), instanceMap );
1028     }
1029   }
1030 }
1031
1032
1033 void Control::Impl::RemoveVisual( RegisteredVisualContainer& visuals, const std::string& visualName )
1034 {
1035   Actor self( mControlImpl.Self() );
1036
1037   for ( RegisteredVisualContainer::Iterator visualIter = visuals.Begin();
1038         visualIter != visuals.End(); ++visualIter )
1039   {
1040     Toolkit::Visual::Base visual = (*visualIter)->visual;
1041     if( visual && visual.GetName() == visualName )
1042     {
1043       Toolkit::GetImplementation(visual).SetOffStage( self );
1044       (*visualIter)->visual.Reset();
1045       visuals.Erase( visualIter );
1046       break;
1047     }
1048   }
1049 }
1050
1051 void Control::Impl::RemoveVisuals( RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals )
1052 {
1053   Actor self( mControlImpl.Self() );
1054   for( DictionaryKeys::iterator iter = removeVisuals.begin(); iter != removeVisuals.end(); ++iter )
1055   {
1056     const std::string visualName = *iter;
1057     RemoveVisual( visuals, visualName );
1058   }
1059 }
1060
1061 void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange,
1062                              Dictionary<Property::Map>& instancedProperties )
1063 {
1064   Dali::CustomActor handle( mControlImpl.GetOwner() );
1065   for( Dictionary<Property::Map>::iterator iter = stateVisualsToChange.Begin();
1066        iter != stateVisualsToChange.End(); ++iter )
1067   {
1068     const std::string& visualName = (*iter).key;
1069     const Property::Map& toMap = (*iter).entry;
1070
1071     // is it a candidate for re-creation?
1072     bool recreate = false;
1073
1074     Toolkit::Visual::Base visual = GetVisualByName( mVisuals, visualName );
1075     if( visual )
1076     {
1077       Property::Map fromMap;
1078       visual.CreatePropertyMap( fromMap );
1079
1080       Toolkit::DevelVisual::Type fromType = GetVisualTypeFromMap( fromMap );
1081       Toolkit::DevelVisual::Type toType = GetVisualTypeFromMap( toMap );
1082
1083       if( fromType != toType )
1084       {
1085         recreate = true;
1086       }
1087       else
1088       {
1089         if( fromType == Toolkit::DevelVisual::IMAGE || fromType == Toolkit::DevelVisual::N_PATCH
1090             || fromType == Toolkit::DevelVisual::SVG || fromType == Toolkit::DevelVisual::ANIMATED_IMAGE )
1091         {
1092           Property::Value* fromUrl = fromMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1093           Property::Value* toUrl = toMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
1094
1095           if( fromUrl && toUrl )
1096           {
1097             std::string fromUrlString;
1098             std::string toUrlString;
1099             fromUrl->Get(fromUrlString);
1100             toUrl->Get(toUrlString);
1101
1102             if( fromUrlString != toUrlString )
1103             {
1104               recreate = true;
1105             }
1106           }
1107         }
1108       }
1109
1110       const Property::Map* instancedMap = instancedProperties.FindConst( visualName );
1111       if( recreate || instancedMap )
1112       {
1113         RemoveVisual( mVisuals, visualName );
1114         Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1115       }
1116       else
1117       {
1118         // @todo check to see if we can apply toMap without recreating the visual
1119         // e.g. by setting only animatable properties
1120         // For now, recreate all visuals, but merge in instance data.
1121         RemoveVisual( mVisuals, visualName );
1122         Style::ApplyVisual( handle, visualName, toMap, instancedMap );
1123       }
1124     }
1125   }
1126 }
1127
1128 void Control::Impl::ReplaceStateVisualsAndProperties( const StylePtr oldState, const StylePtr newState, const std::string& subState )
1129 {
1130   // Collect all old visual names
1131   DictionaryKeys stateVisualsToRemove;
1132   if( oldState )
1133   {
1134     oldState->visuals.GetKeys( stateVisualsToRemove );
1135     if( ! subState.empty() )
1136     {
1137       const StylePtr* oldSubState = oldState->subStates.FindConst(subState);
1138       if( oldSubState )
1139       {
1140         DictionaryKeys subStateVisualsToRemove;
1141         (*oldSubState)->visuals.GetKeys( subStateVisualsToRemove );
1142         Merge( stateVisualsToRemove, subStateVisualsToRemove );
1143       }
1144     }
1145   }
1146
1147   // Collect all new visual properties
1148   Dictionary<Property::Map> stateVisualsToAdd;
1149   if( newState )
1150   {
1151     stateVisualsToAdd = newState->visuals;
1152     if( ! subState.empty() )
1153     {
1154       const StylePtr* newSubState = newState->subStates.FindConst(subState);
1155       if( newSubState )
1156       {
1157         stateVisualsToAdd.Merge( (*newSubState)->visuals );
1158       }
1159     }
1160   }
1161
1162   // If a name is in both add/remove, move it to change list.
1163   Dictionary<Property::Map> stateVisualsToChange;
1164   FindChangableVisuals( stateVisualsToAdd, stateVisualsToChange, stateVisualsToRemove);
1165
1166   // Copy instanced properties (e.g. text label) of current visuals
1167   Dictionary<Property::Map> instancedProperties;
1168   CopyInstancedProperties( mVisuals, instancedProperties );
1169
1170   // For each visual in remove list, remove from mVisuals
1171   RemoveVisuals( mVisuals, stateVisualsToRemove );
1172
1173   // For each visual in add list, create and add to mVisuals
1174   Dali::CustomActor handle( mControlImpl.GetOwner() );
1175   Style::ApplyVisuals( handle, stateVisualsToAdd, instancedProperties );
1176
1177   // For each visual in change list, if it requires a new visual,
1178   // remove old visual, create and add to mVisuals
1179   RecreateChangedVisuals( stateVisualsToChange, instancedProperties );
1180 }
1181
1182 void Control::Impl::SetState( DevelControl::State newState, bool withTransitions )
1183 {
1184   DevelControl::State oldState = mState;
1185   Dali::CustomActor handle( mControlImpl.GetOwner() );
1186   DALI_LOG_INFO(gLogFilter, Debug::Concise, "Control::Impl::SetState: %s\n",
1187                 (mState == DevelControl::NORMAL ? "NORMAL" :(
1188                   mState == DevelControl::FOCUSED ?"FOCUSED" : (
1189                     mState == DevelControl::DISABLED?"DISABLED":"NONE" ))));
1190
1191   if( mState != newState )
1192   {
1193     // If mState was Disabled, and new state is Focused, should probably
1194     // store that fact, e.g. in another property that FocusManager can access.
1195     mState = newState;
1196
1197     // Trigger state change and transitions
1198     // Apply new style, if stylemanager is available
1199     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1200     if( styleManager )
1201     {
1202       const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1203
1204       if( stylePtr )
1205       {
1206         std::string oldStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( oldState, ControlStateTable, ControlStateTableCount );
1207         std::string newStateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( newState, ControlStateTable, ControlStateTableCount );
1208
1209         const StylePtr* newStateStyle = stylePtr->subStates.Find( newStateName );
1210         const StylePtr* oldStateStyle = stylePtr->subStates.Find( oldStateName );
1211         if( oldStateStyle && newStateStyle )
1212         {
1213           // Only change if both state styles exist
1214           ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, mSubStateName );
1215         }
1216       }
1217     }
1218   }
1219 }
1220
1221 void Control::Impl::SetSubState( const std::string& subStateName, bool withTransitions )
1222 {
1223   if( mSubStateName != subStateName )
1224   {
1225     // Get existing sub-state visuals, and unregister them
1226     Dali::CustomActor handle( mControlImpl.GetOwner() );
1227
1228     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
1229     if( styleManager )
1230     {
1231       const StylePtr stylePtr = GetImpl( styleManager ).GetRecordedStyle( Toolkit::Control( mControlImpl.GetOwner() ) );
1232       if( stylePtr )
1233       {
1234         // Stringify state
1235         std::string stateName = Scripting::GetEnumerationName< Toolkit::DevelControl::State >( mState, ControlStateTable, ControlStateTableCount );
1236
1237         const StylePtr* state = stylePtr->subStates.Find( stateName );
1238         if( state )
1239         {
1240           StylePtr stateStyle(*state);
1241
1242           const StylePtr* newStateStyle = stateStyle->subStates.Find( subStateName );
1243           const StylePtr* oldStateStyle = stateStyle->subStates.Find( mSubStateName );
1244           if( oldStateStyle && newStateStyle )
1245           {
1246             std::string empty;
1247             ReplaceStateVisualsAndProperties( *oldStateStyle, *newStateStyle, empty );
1248           }
1249         }
1250       }
1251     }
1252
1253     mSubStateName = subStateName;
1254   }
1255 }
1256
1257 } // namespace Internal
1258
1259 } // namespace Toolkit
1260
1261 } // namespace Dali