Merge "Resource ready signal for Controls (for ImageLoading)" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-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 <dali-toolkit/public-api/controls/control-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23 #include <limits>
24 #include <stack>
25 #include <typeinfo>
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/size-negotiation/relayout-container.h>
29 #include <dali/devel-api/object/handle-devel.h>
30 #include <dali/devel-api/scripting/scripting.h>
31 #include <dali/integration-api/debug.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
35 #include <dali-toolkit/public-api/controls/control.h>
36 #include <dali-toolkit/public-api/styling/style-manager.h>
37 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
38 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
39 #include <dali-toolkit/devel-api/controls/control-devel.h>
40 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
41 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
42 #include <dali-toolkit/internal/styling/style-manager-impl.h>
43 #include <dali-toolkit/internal/visuals/color/color-visual.h>
44 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
45 #include <dali-toolkit/devel-api/align-enums.h>
46 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
47
48 namespace Dali
49 {
50
51 namespace Toolkit
52 {
53
54 namespace Internal
55 {
56
57 namespace
58 {
59
60 #if defined(DEBUG_ENABLED)
61 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_CONTROL_VISUALS");
62 #endif
63
64 DALI_ENUM_TO_STRING_TABLE_BEGIN( CLIPPING_MODE )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, DISABLED )
66 DALI_ENUM_TO_STRING_WITH_SCOPE( ClippingMode, CLIP_CHILDREN )
67 DALI_ENUM_TO_STRING_TABLE_END( CLIPPING_MODE )
68
69 /**
70  *  Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
71  */
72 bool FindVisual( Property::Index targetIndex, RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
73 {
74   for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
75   {
76     if ( (*iter)->index ==  targetIndex )
77     {
78       return true;
79     }
80   }
81   return false;
82 }
83
84 Toolkit::Visual::Base GetVisualByName(
85   RegisteredVisualContainer& visuals,
86   const std::string& visualName )
87 {
88   Toolkit::Visual::Base visualHandle;
89
90   RegisteredVisualContainer::Iterator iter;
91   for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
92   {
93     Toolkit::Visual::Base visual = (*iter)->visual;
94     if( visual && visual.GetName() == visualName )
95     {
96       visualHandle = visual;
97       break;
98     }
99   }
100   return visualHandle;
101 }
102
103 } // unnamed namespace
104
105
106
107 Toolkit::Control Control::New()
108 {
109   // Create the implementation, temporarily owned on stack
110   IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
111
112   // Pass ownership to handle
113   Toolkit::Control handle( *controlImpl );
114
115   // Second-phase init of the implementation
116   // This can only be done after the CustomActor connection has been made...
117   controlImpl->Initialize();
118
119   return handle;
120 }
121
122 void Control::SetStyleName( const std::string& styleName )
123 {
124   if( styleName != mImpl->mStyleName )
125   {
126     mImpl->mStyleName = styleName;
127
128     // Apply new style, if stylemanager is available
129     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
130     if( styleManager )
131     {
132       GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
133     }
134   }
135 }
136
137 const std::string& Control::GetStyleName() const
138 {
139   return mImpl->mStyleName;
140 }
141
142 void Control::SetBackgroundColor( const Vector4& color )
143 {
144   mImpl->mBackgroundColor = color;
145   Property::Map map;
146   map[ Toolkit::DevelVisual::Property::TYPE ] = Toolkit::Visual::COLOR;
147   map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
148
149   SetBackground( map );
150 }
151
152 Vector4 Control::GetBackgroundColor() const
153 {
154   return mImpl->mBackgroundColor;
155 }
156
157 void Control::SetBackground( const Property::Map& map )
158 {
159   Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
160   if( visual )
161   {
162     RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
163     visual.SetDepthIndex( DepthIndex::BACKGROUND );
164
165     // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
166     RelayoutRequest();
167   }
168 }
169
170 void Control::SetBackgroundImage( Image image )
171 {
172   Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
173   if( visual )
174   {
175     RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
176     visual.SetDepthIndex( DepthIndex::BACKGROUND );
177   }
178 }
179
180 void Control::ClearBackground()
181 {
182    UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
183    mImpl->mBackgroundColor = Color::TRANSPARENT;
184
185    // Trigger a size negotiation request that may be needed when unregistering a visual.
186    RelayoutRequest();
187 }
188
189 void Control::EnableGestureDetection(Gesture::Type type)
190 {
191   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
192   {
193     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
194     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
195     mImpl->mPinchGestureDetector.Attach(Self());
196   }
197
198   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
199   {
200     mImpl->mPanGestureDetector = PanGestureDetector::New();
201     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
202     mImpl->mPanGestureDetector.Attach(Self());
203   }
204
205   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
206   {
207     mImpl->mTapGestureDetector = TapGestureDetector::New();
208     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
209     mImpl->mTapGestureDetector.Attach(Self());
210   }
211
212   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
213   {
214     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
215     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
216     mImpl->mLongPressGestureDetector.Attach(Self());
217   }
218 }
219
220 void Control::DisableGestureDetection(Gesture::Type type)
221 {
222   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
223   {
224     mImpl->mPinchGestureDetector.Detach(Self());
225     mImpl->mPinchGestureDetector.Reset();
226   }
227
228   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
229   {
230     mImpl->mPanGestureDetector.Detach(Self());
231     mImpl->mPanGestureDetector.Reset();
232   }
233
234   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
235   {
236     mImpl->mTapGestureDetector.Detach(Self());
237     mImpl->mTapGestureDetector.Reset();
238   }
239
240   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
241   {
242     mImpl->mLongPressGestureDetector.Detach(Self());
243     mImpl->mLongPressGestureDetector.Reset();
244   }
245 }
246
247 PinchGestureDetector Control::GetPinchGestureDetector() const
248 {
249   return mImpl->mPinchGestureDetector;
250 }
251
252 PanGestureDetector Control::GetPanGestureDetector() const
253 {
254   return mImpl->mPanGestureDetector;
255 }
256
257 TapGestureDetector Control::GetTapGestureDetector() const
258 {
259   return mImpl->mTapGestureDetector;
260 }
261
262 LongPressGestureDetector Control::GetLongPressGestureDetector() const
263 {
264   return mImpl->mLongPressGestureDetector;
265 }
266
267 void Control::SetKeyboardNavigationSupport(bool isSupported)
268 {
269   mImpl->mIsKeyboardNavigationSupported = isSupported;
270 }
271
272 bool Control::IsKeyboardNavigationSupported()
273 {
274   return mImpl->mIsKeyboardNavigationSupported;
275 }
276
277 void Control::SetKeyInputFocus()
278 {
279   if( Self().OnStage() )
280   {
281     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
282   }
283 }
284
285 bool Control::HasKeyInputFocus()
286 {
287   bool result = false;
288   if( Self().OnStage() )
289   {
290     Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
291     if( Self() == control )
292     {
293       result = true;
294     }
295   }
296   return result;
297 }
298
299 void Control::ClearKeyInputFocus()
300 {
301   if( Self().OnStage() )
302   {
303     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
304   }
305 }
306
307 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
308 {
309   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
310
311   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
312   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
313 }
314
315 bool Control::IsKeyboardFocusGroup()
316 {
317   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
318 }
319
320 void Control::AccessibilityActivate()
321 {
322   // Inform deriving classes
323   OnAccessibilityActivated();
324 }
325
326 void Control::KeyboardEnter()
327 {
328   // Inform deriving classes
329   OnKeyboardEnter();
330 }
331
332 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
333 {
334   RegisterVisual( index, visual, true );
335 }
336
337 void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
338 {
339   bool visualReplaced ( false );
340   Actor self = Self();
341
342   if( !mImpl->mVisuals.Empty() )
343   {
344     RegisteredVisualContainer::Iterator iter;
345     // Check if visual (index) is already registered.  Replace if so.
346     if ( FindVisual( index, mImpl->mVisuals, iter ) )
347     {
348       if( (*iter)->visual && self.OnStage() )
349       {
350         Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
351       }
352
353       mImpl->StopObservingVisual( (*iter)->visual );
354       mImpl->StartObservingVisual( visual );
355
356       (*iter)->visual = visual;
357       visualReplaced = true;
358     }
359   }
360
361   // If not set, set the name of the visual to the same name as the control's property.
362   // ( If the control has been type registered )
363   if( visual.GetName().empty() )
364   {
365     // Check if the control has been type registered:
366     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( typeid(*this) );
367     if( typeInfo )
368     {
369       // Check if the property index has been registered:
370       Property::IndexContainer indices;
371       typeInfo.GetPropertyIndices( indices );
372       Property::IndexContainer::Iterator iter = std::find( indices.Begin(), indices.End(), index );
373       if( iter != indices.End() )
374       {
375         // If it has, then get it's name and use that for the visual
376         std::string visualName = typeInfo.GetPropertyName( index );
377         visual.SetName( visualName );
378       }
379     }
380   }
381
382   if( !visualReplaced ) // New registration entry
383   {
384     mImpl->mVisuals.PushBack( new RegisteredVisual( index, visual, enabled ) );
385
386     // monitor when the visuals resources are ready
387     mImpl->StartObservingVisual( visual );
388
389   }
390
391   if( visual && self.OnStage() && enabled )
392   {
393     Toolkit::GetImplementation(visual).SetOnStage( self );
394   }
395
396   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n",  visual.GetName().c_str(), index, enabled?"T":"F" );
397 }
398
399 void Control::UnregisterVisual( Property::Index index )
400 {
401    RegisteredVisualContainer::Iterator iter;
402    if ( FindVisual( index, mImpl->mVisuals, iter ) )
403    {
404      // stop observing visual
405      mImpl->StopObservingVisual( (*iter)->visual );
406
407      Actor self( Self() );
408      Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
409      (*iter)->visual.Reset();
410      mImpl->mVisuals.Erase( iter );
411    }
412 }
413
414 Toolkit::Visual::Base Control::GetVisual( Property::Index index ) const
415 {
416   RegisteredVisualContainer::Iterator iter;
417   if ( FindVisual( index, mImpl->mVisuals, iter ) )
418   {
419     return (*iter)->visual;
420   }
421
422   return Toolkit::Visual::Base();
423 }
424
425 void Control::EnableVisual( Property::Index index, bool enable )
426 {
427   RegisteredVisualContainer::Iterator iter;
428   if ( FindVisual( index, mImpl->mVisuals, iter ) )
429   {
430     if (  (*iter)->enabled == enable )
431     {
432       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
433       return;
434     }
435
436     (*iter)->enabled = enable;
437     Actor parentActor = Self();
438     if ( Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
439     {
440       if ( enable )
441       {
442         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
443         Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
444       }
445       else
446       {
447         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
448         Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor );  // No need to call if control not staged.
449       }
450     }
451   }
452 }
453
454 bool Control::IsVisualEnabled( Property::Index index ) const
455 {
456   RegisteredVisualContainer::Iterator iter;
457   if ( FindVisual( index, mImpl->mVisuals, iter ) )
458   {
459     return (*iter)->enabled;
460   }
461   return false;
462 }
463
464 Dali::Animation Control::CreateTransition( const Toolkit::TransitionData& handle )
465 {
466   Dali::Animation transition;
467   const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
468
469   if( transitionData.Count() > 0 )
470   {
471     // Setup a Transition from TransitionData.
472     TransitionData::Iterator end = transitionData.End();
473     for( TransitionData::Iterator iter = transitionData.Begin() ;
474          iter != end; ++iter )
475     {
476       TransitionData::Animator* animator = (*iter);
477
478       Toolkit::Visual::Base visual = GetVisualByName( mImpl->mVisuals, animator->objectName );
479
480       if( visual )
481       {
482         Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
483         visualImpl.AnimateProperty( transition, *animator );
484       }
485       else
486       {
487         // Otherwise, try any actor children of control (Including the control)
488         Actor child = Self().FindChildByName( animator->objectName );
489         if( child )
490         {
491           Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
492           if( propertyIndex != Property::INVALID_INDEX )
493           {
494             if( animator->animate == false )
495             {
496               if( animator->targetValue.GetType() != Property::NONE )
497               {
498                 child.SetProperty( propertyIndex, animator->targetValue );
499               }
500             }
501             else // animate the property
502             {
503               if( animator->initialValue.GetType() != Property::NONE )
504               {
505                 child.SetProperty( propertyIndex, animator->initialValue );
506               }
507
508               if( ! transition )
509               {
510                 transition = Dali::Animation::New( 0.1f );
511               }
512
513               transition.AnimateTo( Property( child, propertyIndex ),
514                                     animator->targetValue,
515                                     animator->alphaFunction,
516                                     TimePeriod( animator->timePeriodDelay,
517                                                 animator->timePeriodDuration ) );
518             }
519           }
520         }
521       }
522     }
523   }
524
525   return transition;
526 }
527
528 bool Control::OnAccessibilityActivated()
529 {
530   return false; // Accessibility activation is not handled by default
531 }
532
533 bool Control::OnKeyboardEnter()
534 {
535   return false; // Keyboard enter is not handled by default
536 }
537
538 bool Control::OnAccessibilityPan(PanGesture gesture)
539 {
540   return false; // Accessibility pan gesture is not handled by default
541 }
542
543 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
544 {
545   return false; // Accessibility touch event is not handled by default
546 }
547
548 bool Control::OnAccessibilityValueChange(bool isIncrease)
549 {
550   return false; // Accessibility value change action is not handled by default
551 }
552
553 bool Control::OnAccessibilityZoom()
554 {
555   return false; // Accessibility zoom action is not handled by default
556 }
557
558 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
559 {
560   return Actor();
561 }
562
563 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
564 {
565 }
566
567 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
568 {
569   return mImpl->mKeyEventSignal;
570 }
571
572 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
573 {
574   return mImpl->mKeyInputFocusGainedSignal;
575 }
576
577 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
578 {
579   return mImpl->mKeyInputFocusLostSignal;
580 }
581
582 bool Control::EmitKeyEventSignal( const KeyEvent& event )
583 {
584   // Guard against destruction during signal emission
585   Dali::Toolkit::Control handle( GetOwner() );
586
587   bool consumed = false;
588
589   // signals are allocated dynamically when someone connects
590   if ( !mImpl->mKeyEventSignal.Empty() )
591   {
592     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
593   }
594
595   if (!consumed)
596   {
597     // Notification for derived classes
598     consumed = OnKeyEvent(event);
599   }
600
601   return consumed;
602 }
603
604 Control::Control( ControlBehaviour behaviourFlags )
605 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
606   mImpl(new Impl(*this))
607 {
608   mImpl->mFlags = behaviourFlags;
609 }
610
611 Control::~Control()
612 {
613   delete mImpl;
614 }
615
616 void Control::Initialize()
617 {
618   // Call deriving classes so initialised before styling is applied to them.
619   OnInitialize();
620
621   if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
622       !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
623   {
624     Toolkit::StyleManager styleManager = StyleManager::Get();
625
626     // if stylemanager is available
627     if( styleManager )
628     {
629       StyleManager& styleManagerImpl = GetImpl( styleManager );
630
631       // Register for style changes
632       styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
633
634       // Apply the current style
635       styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
636     }
637   }
638
639   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
640   {
641     SetKeyboardNavigationSupport( true );
642   }
643 }
644
645 void Control::OnInitialize()
646 {
647 }
648
649 void Control::OnControlChildAdd( Actor& child )
650 {
651 }
652
653 void Control::OnControlChildRemove( Actor& child )
654 {
655 }
656
657 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
658 {
659   // By default the control is only interested in theme (not font) changes
660   if( styleManager && change == StyleChange::THEME_CHANGE )
661   {
662     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
663     RelayoutRequest();
664   }
665 }
666
667 void Control::OnPinch(const PinchGesture& pinch)
668 {
669   if( !( mImpl->mStartingPinchScale ) )
670   {
671     // lazy allocate
672     mImpl->mStartingPinchScale = new Vector3;
673   }
674
675   if( pinch.state == Gesture::Started )
676   {
677     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
678   }
679
680   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
681 }
682
683 void Control::OnPan( const PanGesture& pan )
684 {
685 }
686
687 void Control::OnTap(const TapGesture& tap)
688 {
689 }
690
691 void Control::OnLongPress( const LongPressGesture& longPress )
692 {
693 }
694
695 void Control::EmitKeyInputFocusSignal( bool focusGained )
696 {
697   Dali::Toolkit::Control handle( GetOwner() );
698
699   if ( focusGained )
700   {
701     // signals are allocated dynamically when someone connects
702     if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
703     {
704       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
705     }
706   }
707   else
708   {
709     // signals are allocated dynamically when someone connects
710     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
711     {
712       mImpl->mKeyInputFocusLostSignal.Emit( handle );
713     }
714   }
715 }
716
717 void Control::OnStageConnection( int depth )
718 {
719   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n",  mImpl->mVisuals.Size() );
720
721   Actor self( Self() );
722
723   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
724   {
725     // Check whether the visual is empty and enabled
726     if( (*iter)->visual && (*iter)->enabled )
727     {
728       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
729       Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
730     }
731   }
732
733   if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() )
734   {
735     Property::Value clippingValue = self.GetProperty( Actor::Property::CLIPPING_MODE );
736     int clippingMode = ClippingMode::DISABLED;
737     if( clippingValue.Get( clippingMode ) )
738     {
739       // Add a transparent background if we do not have any renderers or visuals so we clip our children
740
741       if( clippingMode == ClippingMode::CLIP_CHILDREN )
742       {
743         // Create a transparent background visual which will also get staged.
744         SetBackgroundColor( Color::TRANSPARENT );
745       }
746     }
747   }
748 }
749
750 void Control::OnStageDisconnection()
751 {
752   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
753   {
754     // Check whether the visual is empty
755     if( (*iter)->visual )
756     {
757       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
758       Actor self( Self() );
759       Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
760     }
761   }
762 }
763
764 void Control::OnKeyInputFocusGained()
765 {
766   EmitKeyInputFocusSignal( true );
767 }
768
769 void Control::OnKeyInputFocusLost()
770 {
771   EmitKeyInputFocusSignal( false );
772 }
773
774 void Control::OnChildAdd(Actor& child)
775 {
776   // Notify derived classes.
777   OnControlChildAdd( child );
778 }
779
780 void Control::OnChildRemove(Actor& child)
781 {
782   // Notify derived classes.
783   OnControlChildRemove( child );
784 }
785
786 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
787 {
788   Actor self( Self() );
789   if( index == Actor::Property::CLIPPING_MODE )
790   {
791     // Only set the background if we're already on the stage and have no renderers or visuals
792
793     if( mImpl->mVisuals.Empty() && ! self.GetRendererCount() && self.OnStage() )
794     {
795       ClippingMode::Type clippingMode = ClippingMode::DISABLED;
796       if( Scripting::GetEnumerationProperty< ClippingMode::Type >( propertyValue, CLIPPING_MODE_TABLE, CLIPPING_MODE_TABLE_COUNT, clippingMode ) )
797       {
798         // Add a transparent background if we do not have one so we clip children
799
800         if( clippingMode == ClippingMode::CLIP_CHILDREN )
801         {
802           SetBackgroundColor( Color::TRANSPARENT );
803         }
804       }
805     }
806   }
807 }
808
809 void Control::OnSizeSet(const Vector3& targetSize)
810 {
811   Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
812   if( visual )
813   {
814     Vector2 size( targetSize );
815     visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
816   }
817 }
818
819 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
820 {
821   // @todo size negotiate background to new size, animate as well?
822 }
823
824 bool Control::OnTouchEvent(const TouchEvent& event)
825 {
826   return false; // Do not consume
827 }
828
829 bool Control::OnHoverEvent(const HoverEvent& event)
830 {
831   return false; // Do not consume
832 }
833
834 bool Control::OnKeyEvent(const KeyEvent& event)
835 {
836   return false; // Do not consume
837 }
838
839 bool Control::OnWheelEvent(const WheelEvent& event)
840 {
841   return false; // Do not consume
842 }
843
844 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
845 {
846   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
847   {
848     container.Add( Self().GetChildAt( i ), size );
849   }
850
851   Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
852   if( visual )
853   {
854     visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
855   }
856 }
857
858 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
859 {
860 }
861
862 Vector3 Control::GetNaturalSize()
863 {
864   Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
865   if( visual )
866   {
867     Vector2 naturalSize;
868     visual.GetNaturalSize( naturalSize );
869     return Vector3( naturalSize );
870   }
871   return Vector3::ZERO;
872 }
873
874 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
875 {
876   return CalculateChildSizeBase( child, dimension );
877 }
878
879 float Control::GetHeightForWidth( float width )
880 {
881   return GetHeightForWidthBase( width );
882 }
883
884 float Control::GetWidthForHeight( float height )
885 {
886   return GetWidthForHeightBase( height );
887 }
888
889 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
890 {
891   return RelayoutDependentOnChildrenBase( dimension );
892 }
893
894 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
895 {
896 }
897
898 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
899 {
900 }
901
902 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
903 {
904   mImpl->SignalConnected( slotObserver, callback );
905 }
906
907 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
908 {
909   mImpl->SignalDisconnected( slotObserver, callback );
910 }
911
912 Control& GetImplementation( Dali::Toolkit::Control& handle )
913 {
914   CustomActorImpl& customInterface = handle.GetImplementation();
915   // downcast to control
916   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
917   return impl;
918 }
919
920 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
921 {
922   const CustomActorImpl& customInterface = handle.GetImplementation();
923   // downcast to control
924   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
925   return impl;
926 }
927
928 } // namespace Internal
929
930 } // namespace Toolkit
931
932 } // namespace Dali