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