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