[Tizen] Fix ImageView Padding and Margin issues
[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/scripting/scripting.h>
30 #include <dali/integration-api/debug.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/public-api/align-enumerations.h>
34 #include <dali-toolkit/public-api/controls/control.h>
35 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.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/public-api/visuals/visual-properties.h>
39 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
40 #include <dali-toolkit/devel-api/controls/control-devel.h>
41 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
42 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
43 #include <dali-toolkit/internal/styling/style-manager-impl.h>
44 #include <dali-toolkit/internal/visuals/color/color-visual.h>
45 #include <dali-toolkit/internal/visuals/visual-string-constants.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 /**
65  * @brief Replace the background visual if it's a color visual with the renderIfTransparent property set as required.
66  * @param[in] controlImpl The control implementation
67  * @param[in] renderIfTransaparent Whether we should render if the color is transparent
68  */
69 void ChangeBackgroundColorVisual( Control& controlImpl, bool renderIfTransparent )
70 {
71   Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
72
73   Toolkit::Visual::Base backgroundVisual = controlDataImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
74   if( backgroundVisual )
75   {
76     Property::Map map;
77     backgroundVisual.CreatePropertyMap( map );
78     Property::Value* typeValue = map.Find( Toolkit::Visual::Property::TYPE );
79     if( typeValue && typeValue->Get< int >() == Toolkit::Visual::COLOR )
80     {
81       // Only change it if it's a color visual
82       map[ Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT ] = renderIfTransparent;
83       controlImpl.SetBackground( map );
84     }
85   }
86 }
87
88 /**
89  * @brief Creates a clipping renderer if required.
90  * (EG. If no renders exist and clipping is enabled).
91  * @param[in] controlImpl The control implementation.
92  */
93 void CreateClippingRenderer( Control& controlImpl )
94 {
95   // We want to add a transparent background if we do not have one for clipping.
96   Actor self( controlImpl.Self() );
97   int clippingMode = ClippingMode::DISABLED;
98   if( self.GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) )
99   {
100     switch( clippingMode )
101     {
102       case ClippingMode::CLIP_CHILDREN:
103       {
104         if( self.GetRendererCount() == 0u )
105         {
106           Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
107           if( controlDataImpl.mVisuals.Empty() )
108           {
109             controlImpl.SetBackgroundColor( Color::TRANSPARENT );
110           }
111           else
112           {
113             // We have visuals, check if we've set the background and re-create it to
114             // render even if transparent (only if it's a color visual)
115             ChangeBackgroundColorVisual( controlImpl, true );
116           }
117         }
118         break;
119       }
120
121       case ClippingMode::DISABLED:
122       case ClippingMode::CLIP_TO_BOUNDING_BOX:
123       {
124         // If we have a background visual, check if it's a color visual and remove the render if transparent flag
125         ChangeBackgroundColorVisual( controlImpl, false );
126         break;
127       }
128     }
129   }
130 }
131
132 /**
133  * @brief Sets Control::Property::BACKGROUND visual
134  * @param[in] controlImpl The control implementation
135  * @param[in] visual The control background visual
136  * @param[in] size The current size
137  */
138 void SetBackgroundVisual( Control::Impl& controlImpl, Toolkit::Visual::Base& visual, const Vector2& size )
139 {
140   Property::Map transformMap = Property::Map();
141
142   if( ( ( controlImpl.mMargin.start != 0 ) ||
143         ( controlImpl.mMargin.end != 0 ) ||
144         ( controlImpl.mMargin.top != 0 ) ||
145         ( controlImpl.mMargin.bottom != 0 ) ) &&
146       ( controlImpl.mControlImpl.Self().GetCurrentAnchorPoint() != AnchorPoint::CENTER ) )
147   {
148     transformMap.Add( Toolkit::Visual::Transform::Property::SIZE, size )
149                 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
150                 .Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( controlImpl.mMargin.start, controlImpl.mMargin.top ) )
151                 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
152                 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
153                 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
154   }
155
156   visual.SetTransformAndSize( transformMap, size ); // Send an empty map as we do not want to modify the visual's set transform
157 }
158
159 } // unnamed namespace
160
161
162 Toolkit::Control Control::New()
163 {
164   // Create the implementation, temporarily owned on stack
165   IntrusivePtr<Control> controlImpl = new Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) );
166
167   // Pass ownership to handle
168   Toolkit::Control handle( *controlImpl );
169
170   // Second-phase init of the implementation
171   // This can only be done after the CustomActor connection has been made...
172   controlImpl->Initialize();
173
174   return handle;
175 }
176
177 void Control::SetStyleName( const std::string& styleName )
178 {
179   if( styleName != mImpl->mStyleName )
180   {
181     mImpl->mStyleName = styleName;
182
183     // Apply new style, if stylemanager is available
184     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
185     if( styleManager )
186     {
187       GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
188     }
189   }
190 }
191
192 const std::string& Control::GetStyleName() const
193 {
194   return mImpl->mStyleName;
195 }
196
197 void Control::SetBackgroundColor( const Vector4& color )
198 {
199   mImpl->mBackgroundColor = color;
200   Property::Map map;
201   map[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::COLOR;
202   map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
203
204   int clippingMode = ClippingMode::DISABLED;
205   if( ( Self().GetProperty( Actor::Property::CLIPPING_MODE ).Get( clippingMode ) ) &&
206       ( clippingMode == ClippingMode::CLIP_CHILDREN ) )
207   {
208     // If clipping-mode is set to CLIP_CHILDREN, then force visual to add the render even if transparent
209     map[ Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT ] = true;
210   }
211
212   SetBackground( map );
213 }
214
215 Vector4 Control::GetBackgroundColor() const
216 {
217   return mImpl->mBackgroundColor;
218 }
219
220 void Control::SetBackground( const Property::Map& map )
221 {
222   Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
223   visual.SetName("background");
224   if( visual )
225   {
226     mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
227
228     // Trigger a size negotiation request that may be needed by the new visual to relayout its contents.
229     RelayoutRequest();
230   }
231 }
232
233 void Control::SetBackgroundImage( Image image )
234 {
235   Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
236   if( visual )
237   {
238     mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
239   }
240 }
241
242 void Control::ClearBackground()
243 {
244    mImpl->UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
245    mImpl->mBackgroundColor = Color::TRANSPARENT;
246
247    // Trigger a size negotiation request that may be needed when unregistering a visual.
248    RelayoutRequest();
249 }
250
251 void Control::EnableGestureDetection(Gesture::Type type)
252 {
253   if ( (type & Gesture::Pinch) && !mImpl->mPinchGestureDetector )
254   {
255     mImpl->mPinchGestureDetector = PinchGestureDetector::New();
256     mImpl->mPinchGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PinchDetected);
257     mImpl->mPinchGestureDetector.Attach(Self());
258   }
259
260   if ( (type & Gesture::Pan) && !mImpl->mPanGestureDetector )
261   {
262     mImpl->mPanGestureDetector = PanGestureDetector::New();
263     mImpl->mPanGestureDetector.DetectedSignal().Connect(mImpl, &Impl::PanDetected);
264     mImpl->mPanGestureDetector.Attach(Self());
265   }
266
267   if ( (type & Gesture::Tap) && !mImpl->mTapGestureDetector )
268   {
269     mImpl->mTapGestureDetector = TapGestureDetector::New();
270     mImpl->mTapGestureDetector.DetectedSignal().Connect(mImpl, &Impl::TapDetected);
271     mImpl->mTapGestureDetector.Attach(Self());
272   }
273
274   if ( (type & Gesture::LongPress) && !mImpl->mLongPressGestureDetector )
275   {
276     mImpl->mLongPressGestureDetector = LongPressGestureDetector::New();
277     mImpl->mLongPressGestureDetector.DetectedSignal().Connect(mImpl, &Impl::LongPressDetected);
278     mImpl->mLongPressGestureDetector.Attach(Self());
279   }
280 }
281
282 void Control::DisableGestureDetection(Gesture::Type type)
283 {
284   if ( (type & Gesture::Pinch) && mImpl->mPinchGestureDetector )
285   {
286     mImpl->mPinchGestureDetector.Detach(Self());
287     mImpl->mPinchGestureDetector.Reset();
288   }
289
290   if ( (type & Gesture::Pan) && mImpl->mPanGestureDetector )
291   {
292     mImpl->mPanGestureDetector.Detach(Self());
293     mImpl->mPanGestureDetector.Reset();
294   }
295
296   if ( (type & Gesture::Tap) && mImpl->mTapGestureDetector )
297   {
298     mImpl->mTapGestureDetector.Detach(Self());
299     mImpl->mTapGestureDetector.Reset();
300   }
301
302   if ( (type & Gesture::LongPress) && mImpl->mLongPressGestureDetector)
303   {
304     mImpl->mLongPressGestureDetector.Detach(Self());
305     mImpl->mLongPressGestureDetector.Reset();
306   }
307 }
308
309 PinchGestureDetector Control::GetPinchGestureDetector() const
310 {
311   return mImpl->mPinchGestureDetector;
312 }
313
314 PanGestureDetector Control::GetPanGestureDetector() const
315 {
316   return mImpl->mPanGestureDetector;
317 }
318
319 TapGestureDetector Control::GetTapGestureDetector() const
320 {
321   return mImpl->mTapGestureDetector;
322 }
323
324 LongPressGestureDetector Control::GetLongPressGestureDetector() const
325 {
326   return mImpl->mLongPressGestureDetector;
327 }
328
329 void Control::SetKeyboardNavigationSupport(bool isSupported)
330 {
331   mImpl->mIsKeyboardNavigationSupported = isSupported;
332 }
333
334 bool Control::IsKeyboardNavigationSupported()
335 {
336   return mImpl->mIsKeyboardNavigationSupported;
337 }
338
339 void Control::SetKeyInputFocus()
340 {
341   if( Self().OnStage() )
342   {
343     Toolkit::KeyInputFocusManager::Get().SetFocus(Toolkit::Control::DownCast(Self()));
344   }
345 }
346
347 bool Control::HasKeyInputFocus()
348 {
349   bool result = false;
350   if( Self().OnStage() )
351   {
352     Toolkit::Control control = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
353     if( Self() == control )
354     {
355       result = true;
356     }
357   }
358   return result;
359 }
360
361 void Control::ClearKeyInputFocus()
362 {
363   if( Self().OnStage() )
364   {
365     Toolkit::KeyInputFocusManager::Get().RemoveFocus(Toolkit::Control::DownCast(Self()));
366   }
367 }
368
369 void Control::SetAsKeyboardFocusGroup(bool isFocusGroup)
370 {
371   mImpl->mIsKeyboardFocusGroup = isFocusGroup;
372
373   // The following line will be removed when the deprecated API in KeyboardFocusManager is deleted
374   Toolkit::KeyboardFocusManager::Get().SetAsFocusGroup(Self(), isFocusGroup);
375 }
376
377 bool Control::IsKeyboardFocusGroup()
378 {
379   return Toolkit::KeyboardFocusManager::Get().IsFocusGroup(Self());
380 }
381
382 void Control::AccessibilityActivate()
383 {
384   // Inform deriving classes
385   OnAccessibilityActivated();
386 }
387
388 void Control::KeyboardEnter()
389 {
390   // Inform deriving classes
391   OnKeyboardEnter();
392 }
393
394 bool Control::OnAccessibilityActivated()
395 {
396   return false; // Accessibility activation is not handled by default
397 }
398
399 bool Control::OnKeyboardEnter()
400 {
401   return false; // Keyboard enter is not handled by default
402 }
403
404 bool Control::OnAccessibilityPan(PanGesture gesture)
405 {
406   return false; // Accessibility pan gesture is not handled by default
407 }
408
409 bool Control::OnAccessibilityTouch(const TouchEvent& touchEvent)
410 {
411   return false; // Accessibility touch event is not handled by default
412 }
413
414 bool Control::OnAccessibilityValueChange(bool isIncrease)
415 {
416   return false; // Accessibility value change action is not handled by default
417 }
418
419 bool Control::OnAccessibilityZoom()
420 {
421   return false; // Accessibility zoom action is not handled by default
422 }
423
424 Actor Control::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
425 {
426   return Actor();
427 }
428
429 void Control::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
430 {
431 }
432
433 Toolkit::Control::KeyEventSignalType& Control::KeyEventSignal()
434 {
435   return mImpl->mKeyEventSignal;
436 }
437
438 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusGainedSignal()
439 {
440   return mImpl->mKeyInputFocusGainedSignal;
441 }
442
443 Toolkit::Control::KeyInputFocusSignalType& Control::KeyInputFocusLostSignal()
444 {
445   return mImpl->mKeyInputFocusLostSignal;
446 }
447
448 bool Control::EmitKeyEventSignal( const KeyEvent& event )
449 {
450   // Guard against destruction during signal emission
451   Dali::Toolkit::Control handle( GetOwner() );
452
453   bool consumed = false;
454
455   consumed = mImpl->FilterKeyEvent( event );
456
457   // signals are allocated dynamically when someone connects
458   if ( !consumed && !mImpl->mKeyEventSignal.Empty() )
459   {
460     consumed = mImpl->mKeyEventSignal.Emit( handle, event );
461   }
462
463   if ( !consumed )
464   {
465     // Notification for derived classes
466     consumed = OnKeyEvent(event);
467   }
468
469   return consumed;
470 }
471
472 Control::Control( ControlBehaviour behaviourFlags )
473 : CustomActorImpl( static_cast< ActorFlags >( behaviourFlags ) ),
474   mImpl(new Impl(*this))
475 {
476   mImpl->mFlags = behaviourFlags;
477 }
478
479 Control::~Control()
480 {
481   delete mImpl;
482 }
483
484 void Control::Initialize()
485 {
486   // Call deriving classes so initialised before styling is applied to them.
487   OnInitialize();
488
489   if( (mImpl->mFlags & REQUIRES_STYLE_CHANGE_SIGNALS) ||
490       !(mImpl->mFlags & DISABLE_STYLE_CHANGE_SIGNALS) )
491   {
492     Toolkit::StyleManager styleManager = StyleManager::Get();
493
494     // if stylemanager is available
495     if( styleManager )
496     {
497       StyleManager& styleManagerImpl = GetImpl( styleManager );
498
499       // Register for style changes
500       styleManagerImpl.ControlStyleChangeSignal().Connect( this, &Control::OnStyleChange );
501
502       // Apply the current style
503       styleManagerImpl.ApplyThemeStyleAtInit( Toolkit::Control( GetOwner() ) );
504     }
505   }
506
507   if( mImpl->mFlags & REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )
508   {
509     SetKeyboardNavigationSupport( true );
510   }
511 }
512
513 void Control::OnInitialize()
514 {
515 }
516
517 void Control::OnControlChildAdd( Actor& child )
518 {
519 }
520
521 void Control::OnControlChildRemove( Actor& child )
522 {
523 }
524
525 void Control::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
526 {
527   // By default the control is only interested in theme (not font) changes
528   if( styleManager && change == StyleChange::THEME_CHANGE )
529   {
530     GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
531     RelayoutRequest();
532   }
533 }
534
535 void Control::OnPinch(const PinchGesture& pinch)
536 {
537   if( !( mImpl->mStartingPinchScale ) )
538   {
539     // lazy allocate
540     mImpl->mStartingPinchScale = new Vector3;
541   }
542
543   if( pinch.state == Gesture::Started )
544   {
545     *( mImpl->mStartingPinchScale ) = Self().GetCurrentScale();
546   }
547
548   Self().SetScale( *( mImpl->mStartingPinchScale ) * pinch.scale );
549 }
550
551 void Control::OnPan( const PanGesture& pan )
552 {
553 }
554
555 void Control::OnTap(const TapGesture& tap)
556 {
557 }
558
559 void Control::OnLongPress( const LongPressGesture& longPress )
560 {
561 }
562
563 void Control::EmitKeyInputFocusSignal( bool focusGained )
564 {
565   Dali::Toolkit::Control handle( GetOwner() );
566
567   if ( focusGained )
568   {
569     // signals are allocated dynamically when someone connects
570     if ( !mImpl->mKeyInputFocusGainedSignal.Empty() )
571     {
572       mImpl->mKeyInputFocusGainedSignal.Emit( handle );
573     }
574   }
575   else
576   {
577     // signals are allocated dynamically when someone connects
578     if ( !mImpl->mKeyInputFocusLostSignal.Empty() )
579     {
580       mImpl->mKeyInputFocusLostSignal.Emit( handle );
581     }
582   }
583 }
584
585 void Control::OnStageConnection( int depth )
586 {
587   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n",  mImpl->mVisuals.Size() );
588
589   Actor self( Self() );
590
591   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
592   {
593     // Check whether the visual is empty and enabled
594     if( (*iter)->visual && (*iter)->enabled )
595     {
596       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
597       Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
598     }
599   }
600
601   // The clipping renderer is only created if required.
602   CreateClippingRenderer( *this );
603 }
604
605 void Control::OnStageDisconnection()
606 {
607   mImpl->OnStageDisconnection();
608 }
609
610 void Control::OnKeyInputFocusGained()
611 {
612   EmitKeyInputFocusSignal( true );
613 }
614
615 void Control::OnKeyInputFocusLost()
616 {
617   EmitKeyInputFocusSignal( false );
618 }
619
620 void Control::OnChildAdd(Actor& child)
621 {
622   // Notify derived classes.
623   OnControlChildAdd( child );
624 }
625
626 void Control::OnChildRemove(Actor& child)
627 {
628   // Notify derived classes.
629   OnControlChildRemove( child );
630 }
631
632 void Control::OnPropertySet( Property::Index index, Property::Value propertyValue )
633 {
634   // If the clipping mode has been set, we may need to create a renderer.
635   // Only do this if we are already on-stage as the OnStageConnection will handle the off-stage clipping controls.
636   if( ( index == Actor::Property::CLIPPING_MODE ) && Self().OnStage() )
637   {
638     // Note: This method will handle whether creation of the renderer is required.
639     CreateClippingRenderer( *this );
640   }
641 }
642
643 void Control::OnSizeSet(const Vector3& targetSize)
644 {
645   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
646   if( visual )
647   {
648     Vector2 size( targetSize );
649     SetBackgroundVisual( *mImpl, visual, size );
650
651   }
652 }
653
654 void Control::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
655 {
656   // @todo size negotiate background to new size, animate as well?
657 }
658
659 bool Control::OnTouchEvent(const TouchEvent& event)
660 {
661   return false; // Do not consume
662 }
663
664 bool Control::OnHoverEvent(const HoverEvent& event)
665 {
666   return false; // Do not consume
667 }
668
669 bool Control::OnKeyEvent(const KeyEvent& event)
670 {
671   return false; // Do not consume
672 }
673
674 bool Control::OnWheelEvent(const WheelEvent& event)
675 {
676   return false; // Do not consume
677 }
678
679 void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
680 {
681   for( unsigned int i = 0, numChildren = Self().GetChildCount(); i < numChildren; ++i )
682   {
683     Actor child = Self().GetChildAt( i );
684     Vector2 newChildSize( size );
685
686     // When set the padding or margin on the control, child should be resized and repositioned.
687     if( ( mImpl->mPadding.start != 0 ) || ( mImpl->mPadding.end != 0 ) || ( mImpl->mPadding.top != 0 ) || ( mImpl->mPadding.bottom != 0 ) ||
688         ( mImpl->mMargin.start != 0 ) || ( mImpl->mMargin.end != 0 ) || ( mImpl->mMargin.top != 0 ) || ( mImpl->mMargin.bottom != 0 ) )
689     {
690       Extents padding = mImpl->mPadding;
691
692       Dali::CustomActor ownerActor(GetOwner());
693       Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( ownerActor.GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
694
695       if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
696       {
697         std::swap( padding.start, padding.end );
698       }
699
700       newChildSize.width = size.width - ( padding.start + padding.end );
701       newChildSize.height = size.height - ( padding.top + padding.bottom );
702
703       // Cannot use childs Position property as it can already have padding and margin applied on it,
704       // so we end up cumulatively applying them over and over again.
705       Vector2 childOffset( 0.f, 0.f );
706       childOffset.x += ( mImpl->mMargin.start + padding.start );
707       childOffset.y += ( mImpl->mMargin.top + padding.top );
708
709       child.SetPosition( childOffset.x, childOffset.y );
710     }
711
712     container.Add( child, newChildSize );
713   }
714
715   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
716   if( visual )
717   {
718     SetBackgroundVisual( *mImpl, visual, size );
719   }
720 }
721
722 void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
723 {
724 }
725
726 Vector3 Control::GetNaturalSize()
727 {
728   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
729   if( visual )
730   {
731     Vector2 naturalSize;
732     visual.GetNaturalSize( naturalSize );
733     naturalSize.width += ( mImpl->mPadding.start + mImpl->mPadding.end );
734     naturalSize.height += ( mImpl->mPadding.top + mImpl->mPadding.bottom );
735     return Vector3( naturalSize );
736   }
737   return Vector3::ZERO;
738 }
739
740 float Control::CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
741 {
742   return CalculateChildSizeBase( child, dimension );
743 }
744
745 float Control::GetHeightForWidth( float width )
746 {
747   return GetHeightForWidthBase( width );
748 }
749
750 float Control::GetWidthForHeight( float height )
751 {
752   return GetWidthForHeightBase( height );
753 }
754
755 bool Control::RelayoutDependentOnChildren( Dimension::Type dimension )
756 {
757   return RelayoutDependentOnChildrenBase( dimension );
758 }
759
760 void Control::OnCalculateRelayoutSize( Dimension::Type dimension )
761 {
762 }
763
764 void Control::OnLayoutNegotiated( float size, Dimension::Type dimension )
765 {
766 }
767
768 void Control::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
769 {
770   mImpl->SignalConnected( slotObserver, callback );
771 }
772
773 void Control::SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback )
774 {
775   mImpl->SignalDisconnected( slotObserver, callback );
776 }
777
778 Control& GetImplementation( Dali::Toolkit::Control& handle )
779 {
780   CustomActorImpl& customInterface = handle.GetImplementation();
781   // downcast to control
782   Control& impl = dynamic_cast< Internal::Control& >( customInterface );
783   return impl;
784 }
785
786 const Control& GetImplementation( const Dali::Toolkit::Control& handle )
787 {
788   const CustomActorImpl& customInterface = handle.GetImplementation();
789   // downcast to control
790   const Control& impl = dynamic_cast< const Internal::Control& >( customInterface );
791   return impl;
792 }
793
794 } // namespace Internal
795
796 } // namespace Toolkit
797
798 } // namespace Dali