[NUI] Split NUI Assemblies (#865)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / View.cs
index 6b780c3..4a8e259 100755 (executable)
@@ -15,7 +15,9 @@
  *
  */
 using System;
+using System.Collections.Generic;
 using System.ComponentModel;
+using System.IO;
 using System.Runtime.InteropServices;
 using Tizen.NUI.Binding;
 
@@ -39,16 +41,40 @@ namespace Tizen.NUI.BaseComponents
         RTL
     }
 
+    /// <summary>
+    /// [Draft] Available policies for layout parameters
+    /// </summary>
+    /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public static class LayoutParamPolicies
+    {
+        /// <summary>
+        /// Constant which indicates child size should match parent size
+        /// </summary>
+       /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+       [EditorBrowsable(EditorBrowsableState.Never)]
+        public const int MatchParent = -1;
+        /// <summary>
+        /// Constant which indicates parent should take the smallest size possible to wrap it's children with their desired size
+        /// </summary>
+        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public const int WrapContent = -2;
+    }
+
+    /// <summary>
+    /// [Draft] Replaced by LayoutParamPolicies, will be removed once occurrences replaced.
+    /// </summary>
     internal enum ChildLayoutData
     {
         /// <summary>
         /// Constant which indicates child size should match parent size
         /// </summary>
-        MatchParent = -1,
+        MatchParent = LayoutParamPolicies.MatchParent,
         /// <summary>
         /// Constant which indicates parent should take the smallest size possible to wrap it's children with their desired size
         /// </summary>
-        WrapContent = -2,
+        WrapContent = LayoutParamPolicies.WrapContent,
     }
 
     internal enum ResourceLoadingStatusType
@@ -63,7 +89,7 @@ namespace Tizen.NUI.BaseComponents
     /// View is the base class for all views.
     /// </summary>
     /// <since_tizen> 3 </since_tizen>
-    public class View : Container, IResourcesProvider
+    public class View : Container
     {
         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
@@ -401,23 +427,27 @@ namespace Tizen.NUI.BaseComponents
                 default: return Tizen.NUI.VerticalAlignmentType.Top;
             }
         });
-        /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+
+        /// <summary>
+        /// "Please DO NOT use! This will be deprecated! Please use 'View Weight' instead of BindableProperty"
+        /// This needs to be hidden as inhouse API until all applications using it have been updated.  Do not make public.
+        /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public static readonly BindableProperty WeightProperty = BindableProperty.Create("Weight", typeof(float), typeof(View), default(float), propertyChanged: (bindable, oldValue, newValue) =>
         {
             var view = (View)bindable;
             if (newValue != null)
             {
-                Tizen.NUI.Object.SetProperty(view.swigCPtr, LinearLayout.ChildProperty.WEIGHT, new Tizen.NUI.PropertyValue((float)newValue));
+                view.Weight = (float)newValue;
             }
         },
+
         defaultValueCreator: (bindable) =>
         {
             var view = (View)bindable;
-            float temp = 0.0f;
-            Tizen.NUI.Object.GetProperty(view.swigCPtr, LinearLayout.ChildProperty.WEIGHT).Get(out temp);
-            return temp;
+            return view.Weight;
         });
+
         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
         public static readonly BindableProperty LeftFocusableViewProperty = BindableProperty.Create(nameof(View.LeftFocusableView), typeof(View), typeof(View), null, propertyChanged: (bindable, oldValue, newValue) =>
@@ -1242,20 +1272,30 @@ namespace Tizen.NUI.BaseComponents
             Tizen.NUI.Object.GetProperty(view.swigCPtr, View.Property.MARGIN).Get(temp);
             return temp;
         });
-        /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+
+        /// <summary>
+        /// Flag to indicate if layout set explicitly via API call or View was automatically given a Layout.
+        /// </summary>
+        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly BindableProperty StyleProperty = BindableProperty.Create("Style", typeof(Style), typeof(View), default(Style), propertyChanged: (bindable, oldvalue, newvalue) => ((View)bindable)._mergedStyle.Style = (Style)newvalue);
+        public bool layoutSet = false;
 
-        internal readonly MergedStyle _mergedStyle;
         /// <summary>
         /// Flag to allow Layouting to be disabled for Views.
         /// Once a View has a Layout set then any children added to Views from then on will receive
         /// automatic Layouts.
         /// </summary>
-        private static bool layoutingDisabled = true;
-        private global::System.Runtime.InteropServices.HandleRef swigCPtr;
+        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static bool layoutingDisabled{get; set;} = true;
 
-        private bool layoutSet = false; // Flag to indicate if SetLayout was called or View was automatically given a Layout
+        private global::System.Runtime.InteropServices.HandleRef swigCPtr;
+        private LayoutItem _layout; // Exclusive layout assigned to this View.
+        private int _widthPolicy = LayoutParamPolicies.WrapContent; // Layout width policy
+        private int _heightPolicy = LayoutParamPolicies.WrapContent; // Layout height policy
+        private float _weight = 0.0f; // Weighting of child View in a Layout
+        private MeasureSpecification _measureSpecificationWidth; // Layout width and internal Mode
+        private MeasureSpecification _measureSpecificationHeight; // Layout height and internal Mode
         private bool _backgroundImageSynchronosLoading = false;
         private EventHandler _offWindowEventHandler;
         private OffWindowEventCallbackType _offWindowEventCallback;
@@ -1312,7 +1352,6 @@ namespace Tizen.NUI.BaseComponents
             {
                 PositionUsesPivotPoint = false;
             }
-            _mergedStyle = new MergedStyle(GetType(), this);
 
             _onWindowSendEventCallback = SendViewAddedEventToWindow;
             this.OnWindowSignal().Connect(_onWindowSendEventCallback);
@@ -1354,12 +1393,12 @@ namespace Tizen.NUI.BaseComponents
         /// Event when a child is removed.
         /// </summary>
         /// <since_tizen> 5 </since_tizen>
-        public new event EventHandler<ChildRemovedEventArgs> ChildRemoved;
+        public event EventHandler<ChildRemovedEventArgs> ChildRemoved;
         /// <summary>
         /// Event when a child is added.
         /// </summary>
         /// <since_tizen> 5 </since_tizen>
-        public new event EventHandler<ChildAddedEventArgs> ChildAdded;
+        public event EventHandler<ChildAddedEventArgs> ChildAdded;
 
         /// <summary>
         /// An event for the KeyInputFocusGained signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
@@ -1801,30 +1840,6 @@ namespace Tizen.NUI.BaseComponents
             CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000
         }
 
-        /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public bool IsResourcesCreated
-        {
-            get
-            {
-                return Application.Current.IsResourcesCreated;
-            }
-        }
-
-        /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public ResourceDictionary XamlResources
-        {
-            get
-            {
-                return Application.Current.XamlResources;
-            }
-            set
-            {
-                Application.Current.XamlResources = value;
-            }
-        }
-
         /// <summary>
         /// The StyleName, type string.
         /// </summary>
@@ -2236,8 +2251,8 @@ namespace Tizen.NUI.BaseComponents
                 SetValue(Size2DProperty, value);
                 // Set Specification so when layouts measure this View it matches the value set here.
                 // All Views are currently Layouts.
-                SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue(value.Width));
-                SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue(value.Height));
+                MeasureSpecificationWidth = new MeasureSpecification(new LayoutLength(value.Width), MeasureSpecification.ModeType.Exactly);
+                MeasureSpecificationHeight = new MeasureSpecification(new LayoutLength(value.Height), MeasureSpecification.ModeType.Exactly);
                 NotifyPropertyChanged();
             }
         }
@@ -2319,7 +2334,7 @@ namespace Tizen.NUI.BaseComponents
         /// This is true by default.
         /// </summary>
         /// <remarks>If false, then the top-left of the view is used for the position.
-        /// Setting this to false will allow scaling or rotation around the anchor-point without affecting the view's position.
+        /// Setting this to false will allow scaling or rotation around the pivot point without affecting the view's position.
         /// </remarks>
         /// <since_tizen> 3 </since_tizen>
         public bool PositionUsesPivotPoint
@@ -2502,7 +2517,7 @@ namespace Tizen.NUI.BaseComponents
             set
             {
                 SetValue(SizeWidthProperty, value);
-                SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue(value));
+                WidthSpecification = (int)Math.Ceiling(value);
                 NotifyPropertyChanged();
             }
         }
@@ -2520,7 +2535,7 @@ namespace Tizen.NUI.BaseComponents
             set
             {
                 SetValue(SizeHeightProperty, value);
-                SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue(value));
+                HeightSpecification = (int)Math.Ceiling(value);
                 NotifyPropertyChanged();
             }
         }
@@ -2926,17 +2941,17 @@ namespace Tizen.NUI.BaseComponents
                 {
                     case ResizePolicyType.UseNaturalSize:
                     {
-                        SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue( (int)ChildLayoutData.WrapContent ) );
+                        WidthSpecification = LayoutParamPolicies.WrapContent;
                         break;
                     }
                     case ResizePolicyType.FillToParent:
                     {
-                        SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue((int)ChildLayoutData.MatchParent));
+                        WidthSpecification = LayoutParamPolicies.MatchParent;
                         break;
                     }
                     case ResizePolicyType.FitToChildren:
                     {
-                        SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue((int)ChildLayoutData.WrapContent));
+                        WidthSpecification = LayoutParamPolicies.WrapContent;
                         break;
                     }
                     default:
@@ -2965,17 +2980,17 @@ namespace Tizen.NUI.BaseComponents
                 {
                     case ResizePolicyType.UseNaturalSize:
                     {
-                        SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue( (int)ChildLayoutData.WrapContent ) );
+                        HeightSpecification = LayoutParamPolicies.WrapContent;
                         break;
                     }
                     case ResizePolicyType.FillToParent:
                     {
-                        SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue((int)ChildLayoutData.MatchParent));
+                        HeightSpecification = LayoutParamPolicies.MatchParent;
                         break;
                     }
                     case ResizePolicyType.FitToChildren:
                     {
-                        SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue((int)ChildLayoutData.WrapContent));
+                        HeightSpecification = LayoutParamPolicies.WrapContent;
                         break;
                     }
                     default:
@@ -3045,12 +3060,37 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                return (Extents)GetValue(PaddingProperty);
+                // If View has a Layout then padding in stored in the base Layout class
+                if (Layout !=null)
+                {
+                    return Layout.Padding;
+                }
+                else
+                {
+                    return (Extents)GetValue(PaddingProperty);
+                }
+                // Two return points to prevent creating a zeroed Extent native object before assignment
             }
             set
             {
-                SetValue(PaddingProperty, value);
+                Extents padding = value;
+                if (Layout !=null)
+                {
+                    // Layout set so store Padding in LayoutItem instead of in View.
+                    // If View stores the Padding value then Legacy Size Negotiation will overwrite
+                    // the position and sizes measure in the Layouting.
+                    Layout.Padding = value;
+                    // If Layout is a LayoutItem then it could be a View that handles it's own padding.
+                    // Let the View keeps it's padding.  Still store Padding in Layout to reduce code paths.
+                    if( Layout.GetType() != typeof(LayoutItem)) // If a Layout container of some kind.
+                    {
+                        padding =  new Extents(0,0,0,0); // Reset value stored in View.
+                    }
+                    _layout?.RequestLayout();
+                }
+                SetValue(PaddingProperty, padding);
                 NotifyPropertyChanged();
+                _layout?.RequestLayout();
             }
         }
 
@@ -3066,12 +3106,13 @@ namespace Tizen.NUI.BaseComponents
             }
             set
             {
-                if (Layout != null)
+                if (_layout != null)
                 {
                     // Note: it only works if minimum size is >= than natural size.
                     // To force the size it should be done through the width&height spec or Size2D.
-                    Layout.MinimumWidth = new Tizen.NUI.LayoutLength(value.Width);
-                    Layout.MinimumHeight = new Tizen.NUI.LayoutLength(value.Height);
+                    _layout.MinimumWidth = new Tizen.NUI.LayoutLength(value.Width);
+                    _layout.MinimumHeight = new Tizen.NUI.LayoutLength(value.Height);
+                    _layout.RequestLayout();
                 }
                 SetValue(MinimumSizeProperty, value);
                 NotifyPropertyChanged();
@@ -3092,6 +3133,14 @@ namespace Tizen.NUI.BaseComponents
             {
                 // We don't have Layout.Maximum(Width|Height) so we cannot apply it to layout.
                 // MATCH_PARENT spec + parent container size can be used to limit
+                if (_layout != null)
+                {
+                    // Note: it only works if minimum size is >= than natural size.
+                    // To force the size it should be done through the width&height spec or Size2D.
+                    _layout.MinimumHeight = new Tizen.NUI.LayoutLength(value.Width);
+                    _layout.MinimumWidth = new Tizen.NUI.LayoutLength(value.Height);
+                    _layout.RequestLayout();
+                }
                 SetValue(MaximumSizeProperty, value);
                 NotifyPropertyChanged();
             }
@@ -3197,8 +3246,8 @@ namespace Tizen.NUI.BaseComponents
                 SetValue(SizeProperty, value);
                 // Set Specification so when layouts measure this View it matches the value set here.
                 // All Views are currently Layouts.
-                SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue(value.Width));
-                SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue(value.Height));
+                WidthSpecification = (int)Math.Ceiling(value.Width);
+                HeightSpecification = (int)Math.Ceiling(value.Height);
                 NotifyPropertyChanged();
             }
         }
@@ -3271,6 +3320,7 @@ namespace Tizen.NUI.BaseComponents
             {
                 SetValue(LayoutDirectionProperty, value);
                 NotifyPropertyChanged();
+                _layout?.RequestLayout();
             }
         }
 
@@ -3286,15 +3336,85 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                return (Extents)GetValue(MarginProperty);
+                // If View has a Layout then margin is stored in Layout.
+                if (Layout != null)
+                {
+                    return Layout.Margin;
+                }
+                else
+                {
+                    // If Layout not set then return margin stored in View.
+                    return (Extents)GetValue(MarginProperty);
+                }
+                // Two return points to prevent creating a zeroed Extent native object before assignment
             }
             set
             {
-                SetValue(MarginProperty, value);
+                if (Layout != null)
+                {
+                    // Layout set so store Margin in LayoutItem instead of View.
+                    // If View stores the Margin too then the Legacy Size Negotiation will
+                    // overwrite the position and size values measured in the Layouting.
+                    Layout.Margin = value;
+                    SetValue(MarginProperty, new Extents(0,0,0,0));
+                    _layout?.RequestLayout();
+                }
+                else
+                {
+                    SetValue(MarginProperty, value);
+                }
                 NotifyPropertyChanged();
+                _layout?.RequestLayout();
             }
         }
 
+        ///<summary>
+        /// The required policy for this dimension, ChildLayoutData enum or exact value.
+        ///</summary>
+        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public int WidthSpecification
+        {
+            get
+            {
+                return _widthPolicy;
+            }
+            set
+            {
+                _widthPolicy = value;
+                if (_widthPolicy >= 0)
+                {
+                    _measureSpecificationWidth = new MeasureSpecification( new LayoutLength(value), MeasureSpecification.ModeType.Exactly );
+                    Size2D.Width = _widthPolicy;
+                }
+                _layout?.RequestLayout();
+            }
+        }
+
+        ///<summary>
+        /// The required policy for this dimension, ChildLayoutData enum or exact value.
+        ///</summary>
+        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public int HeightSpecification
+        {
+            get
+            {
+                return _heightPolicy;
+            }
+            set
+            {
+                _heightPolicy = value;
+                if (_heightPolicy >= 0)
+                {
+                    _measureSpecificationHeight = new MeasureSpecification( new LayoutLength(value), MeasureSpecification.ModeType.Exactly );
+                    Size2D.Height = _heightPolicy;
+                }
+               _layout?.RequestLayout();
+            }
+        }
+
+
         /// <summary>
         /// [Obsolete("Please do not use! this will be deprecated")]
         /// </summary>
@@ -3315,18 +3435,7 @@ namespace Tizen.NUI.BaseComponents
             {
                 SetProperty(View.Property.PADDING, new Tizen.NUI.PropertyValue(value));
                 NotifyPropertyChanged();
-            }
-        }
-
-        internal Style Style
-        {
-            get
-            {
-                return (Style)GetValue(StyleProperty);
-            }
-            set
-            {
-                SetValue(StyleProperty, value);
+                _layout?.RequestLayout();
             }
         }
 
@@ -3337,16 +3446,14 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                int tmp = 0;
-                if (GetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION).Get(out tmp) == false)
-                {
-                    NUILog.Error("WidthSpecificationFixed get error!");
-                }
-                return tmp;
+                return _widthPolicy;
             }
             set
             {
-                SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue(value));
+                _widthPolicy = value;
+                _measureSpecificationWidth = new MeasureSpecification(new LayoutLength(value), MeasureSpecification.ModeType.Exactly);
+                Size2D.Width = value;
+                _layout?.RequestLayout();
             }
         }
 
@@ -3357,16 +3464,14 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                int tmp = 0;
-                if (GetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION).Get(out tmp) == false)
-                {
-                    NUILog.Error("HeightSpecificationFixed get error!");
-                }
-                return tmp;
+                return _heightPolicy;
             }
             set
             {
-                SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue(value));
+                _heightPolicy = value;
+                _measureSpecificationHeight = new MeasureSpecification(new LayoutLength(value), MeasureSpecification.ModeType.Exactly);
+                Size2D.Height = value;
+                _layout?.RequestLayout();
             }
         }
 
@@ -3377,16 +3482,11 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                int tmp = 0;
-                if (GetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION).Get(out tmp) == false)
-                {
-                    NUILog.Error("WidthSpecificationFixed get error!");
-                }
-                return (ChildLayoutData)tmp;
+                return (ChildLayoutData)_widthPolicy;
             }
             set
             {
-                SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new Tizen.NUI.PropertyValue((int)value));
+                _widthPolicy = (int)value;
             }
         }
 
@@ -3397,16 +3497,11 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                int tmp = 0;
-                if (GetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION).Get(out tmp) == false)
-                {
-                    NUILog.Error("HeightSpecificationFixed get error!");
-                }
-                return (ChildLayoutData)tmp;
+                return (ChildLayoutData)_heightPolicy;
             }
             set
             {
-                SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new Tizen.NUI.PropertyValue((int)value));
+                _heightPolicy = (int)value;
             }
         }
 
@@ -3414,12 +3509,12 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                return (float)GetValue(WeightProperty);
+                return _weight;
             }
             set
             {
-                SetValue(WeightProperty, value);
-                NotifyPropertyChanged();
+                _weight = value;
+                _layout.RequestLayout();
             }
         }
 
@@ -3494,7 +3589,7 @@ namespace Tizen.NUI.BaseComponents
         }
 
         /// <summary>
-        /// Set the layout on this control.
+        /// Set the layout on this View. Replaces any existing Layout.
         /// </summary>
         /// <remarks>
         /// </remarks>
@@ -3502,42 +3597,121 @@ namespace Tizen.NUI.BaseComponents
         {
             get
             {
-                IntPtr cPtr = Interop.Layout.GetLayout__SWIG_1(View.getCPtr(this));
-
-                HandleRef CPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
-                BaseHandle basehandle = Registry.GetManagedBaseHandleFromNativePtr(CPtr.Handle);
-                Interop.BaseHandle.delete_BaseHandle(CPtr);
-                CPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
-
-                if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-
-                return basehandle as LayoutItem;
+                return _layout;
             }
             set
             {
-                Log.Info("NUI", "Set Layout on:" + Name + "\n");
+                // Do nothing if layout provided is already set on this View.
+                if (value == _layout)
+                {
+                    return;
+                }
+
+                Log.Info("NUI", "Setting Layout on:" + Name + "\n");
                 layoutingDisabled = false;
                 layoutSet = true;
+
+                // If new layout being set already has a owner then that owner receives a replacement default layout.
+                // First check if the layout to be set already has a owner.
+                if (value?.Owner != null)
+                {
+                    Log.Info("NUI", "Set layout already in use by another View: " + value.Owner.Name + "will get a LayoutGroup\n");
+                    // Previous owner of the layout gets a default layout as a replacement.
+                    value.Owner.Layout = new LayoutGroup();
+
+                    // Copy Margin and Padding to replacement LayoutGroup.
+                    value.Owner.Layout.Margin = value.Margin;
+                    value.Owner.Layout.Padding = value.Padding;
+                }
+
+                // Copy Margin and Padding to new layout being set or restore padding and margin back to
+                // View if no replacement. Previously margin and padding values would have been moved from
+                // the View to the layout.
+                if (_layout != null ) // Existing layout
+                {
+                    if (value != null)
+                    {
+                        // Existing layout being replaced so copy over margin and padding values.
+                        value.Margin = _layout.Margin;
+                        value.Padding = _layout.Padding;
+                    }
+                    else
+                    {
+                      // Layout not being replaced so restore margin and padding to View.
+                      SetValue(MarginProperty, _layout.Margin);
+                      SetValue(PaddingProperty, _layout.Padding);
+                      NotifyPropertyChanged();
+                    }
+                }
+                else
+                {
+                    // First Layout to be added to the View hence copy
+
+                    // Do not try to set Margins or Padding on a null Layout (when a layout is being removed from a View)
+                    if (value !=null)
+                    {
+                        if (Margin.Top != 0 || Margin.Bottom !=0 || Margin.Start !=0 || Margin.End != 0)
+                        {
+                            // If View already has a margin set then store it in Layout instead.
+                            value.Margin = Margin;
+                            SetValue(MarginProperty, new Extents(0,0,0,0));
+                            NotifyPropertyChanged();
+                        }
+
+                        if (Padding.Top != 0 || Padding.Bottom !=0 || Padding.Start !=0 || Padding.End != 0)
+                        {
+                            // If View already has a padding set then store it in Layout instead.
+                            value.Padding = Padding;
+                            SetValue(PaddingProperty, new Extents(0,0,0,0));
+                            NotifyPropertyChanged();
+                        }
+                    }
+                }
+
+                // Remove existing layout from it's parent layout group.
+                _layout?.Unparent();
+
+                // Set layout to this view
                 SetLayout(value);
             }
         }
 
+        internal void SetLayout(LayoutItem layout)
+        {
+            _layout = layout;
+            _layout?.AttachToOwner(this);
+            _layout?.RequestLayout();
+        }
+
         /// <summary>
-        /// Set that layouting is required on this View. It will automatically receive a Layout.
+        /// Stores the calculated width value and its ModeType. Width component.
         /// </summary>
-        /// <remarks>
-        /// </remarks>
-        internal bool LayoutingRequired
+        internal MeasureSpecification MeasureSpecificationWidth
         {
+            set
+            {
+                _measureSpecificationWidth = value;
+                _layout?.RequestLayout();
+            }
             get
             {
-                bool result = Interop.View.View_IsLayoutingRequired(View.getCPtr(this));
-                if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-                return result;
+                return _measureSpecificationWidth;
             }
+        }
+
+        /// <summary>
+        /// Stores the calculated width value and its ModeType. Height component.
+        /// </summary>
+        internal MeasureSpecification MeasureSpecificationHeight
+        {
             set
             {
-                Tizen.NUI.Interop.View.View_SetLayoutingRequired(View.getCPtr(this), value);
+                _measureSpecificationHeight = value;
+                _layout?.RequestLayout();
+            }
+            get
+            {
+                return _measureSpecificationHeight;
             }
         }
 
@@ -3780,52 +3954,27 @@ namespace Tizen.NUI.BaseComponents
         /// <since_tizen> 4 </since_tizen>
         public override void Add(View child)
         {
+            bool hasLayout = (_layout != null);
+            Log.Info("NUI", "Add:" + child.Name + " to View:" + Name + "which has layout[" + hasLayout + "] + \n");
+
             if (null == child)
             {
                 Tizen.Log.Fatal("NUI", "Child is null");
                 return;
             }
 
-            Log.Info("NUI", "Adding Child:" + child.Name + " to " + Name + "\n");
-
             Container oldParent = child.GetParent();
             if (oldParent != this)
             {
+                // If child already has a parent then re-parent child
                 if (oldParent != null)
                 {
                     oldParent.Remove(child);
                 }
                 child.InternalParent = this;
 
-                // Only give children a layout if their parent is an explicit container or a pure View.
-                // Pure View meaning not derived from a View, e.g a Legacy container.
-                // layoutSet flag is true when the View became a layout using the set Layout API opposed to automatically due to it's parent.
-                // First time the set Layout API is used by any View the Window no longer has layoutingDisabled.
-                if ((true == layoutSet || GetType() == typeof(View)) && null == child.Layout && false == layoutingDisabled)
-                {
-                    Log.Info("NUI", "Parent[" + Name + "] Layout set[" + layoutSet.ToString() + "] Pure View[" + (!layoutSet).ToString() + "]\n");
-                    // If child is a View or explicitly set to require layouting then set child as a LayoutGroup.
-                    // If the child is derived from a View then it may be a legacy or existing container hence will do layouting itself.
-                    if (child.GetType() == typeof(View) || true == child.LayoutingRequired)
-                    {
-                        Log.Info("NUI", "Creating LayoutGroup for " + child.Name + " LayoutingRequired[" + child.LayoutingRequired.ToString() + "]\n");
-                        child.SetLayout(new LayoutGroup());
-                    }
-                    else
-                    {
-                        // Adding child as a leaf, layouting will not propagate past this child.
-                        // Legacy containers will be a LayoutItems too and layout their children how they wish.
-                        Log.Info("NUI", "Creating LayoutItem for " + child.Name + "\n");
-                        child.SetLayout(new LayoutItem());
-                    }
-                }
-
-                if (Layout)
-                {
-                    Layout.LayoutChildren.Add(child.Layout);
-                }
-
                 Interop.Actor.Actor_Add(swigCPtr, View.getCPtr(child));
+
                 if (NDalicPINVOKE.SWIGPendingException.Pending)
                     throw NDalicPINVOKE.SWIGPendingException.Retrieve();
                 Children.Add(child);
@@ -3848,6 +3997,9 @@ namespace Tizen.NUI.BaseComponents
         /// <since_tizen> 4 </since_tizen>
         public override void Remove(View child)
         {
+            bool hasLayout = (_layout != null);
+            Log.Info("NUI","Removing View:" + child.Name + "layout[" + hasLayout.ToString() +"]\n");
+
             Interop.Actor.Actor_Remove(swigCPtr, View.getCPtr(child));
             if (NDalicPINVOKE.SWIGPendingException.Pending)
                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -3855,14 +4007,6 @@ namespace Tizen.NUI.BaseComponents
             Children.Remove(child);
             child.InternalParent = null;
 
-            if (Layout)
-            {
-                if (child.Layout)
-                {
-                    Layout.LayoutChildren.Remove(child.Layout);
-                }
-            }
-
             if (ChildRemoved != null)
             {
                 ChildRemovedEventArgs e = new ChildRemovedEventArgs
@@ -3917,8 +4061,16 @@ namespace Tizen.NUI.BaseComponents
         /// <since_tizen> 3 </since_tizen>
         public bool HasFocus()
         {
-            bool ret = Interop.View.View_HasKeyInputFocus(swigCPtr);
-            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            bool ret = false;
+            if (swigCPtr.Handle != global::System.IntPtr.Zero)
+            {
+                ret = Interop.View.View_HasKeyInputFocus(swigCPtr);
+                               if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+            }
+            else
+            {
+                Tizen.Log.Error("NUI", "swigCPtr of view is aleady disposed.");
+            }
             return ret;
         }
 
@@ -5054,14 +5206,12 @@ namespace Tizen.NUI.BaseComponents
             return (IntPtr)swigCPtr;
         }
 
-        internal void SetLayout(LayoutItem layout)
+        /// <summary>
+        /// Removes the layout from this View.
+        /// </summary>
+        internal void ResetLayout()
         {
-            Interop.Layout.SetLayout__SWIG_1(View.getCPtr(this), LayoutItem.getCPtr(layout));
-            layout.LayoutChildren.Clear();
-            foreach (View view in Children)
-            {
-                layout.LayoutChildren.Add(view.Layout);
-            }
+            _layout = null;
         }
 
         internal ResourceLoadingStatusType GetBackgroundResourceStatus()