[NUI] Sync with dalihub & API5 branch (#631)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / RelayoutContainer.cs
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
19 namespace Tizen.NUI
20 {
21     using Tizen.NUI.BaseComponents;
22     /// <summary>
23     /// An interface to encapsulate the information required for relayout.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class RelayoutContainer : global::System.IDisposable
27     {
28         /// <summary>
29         /// swigCMemOwn
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         protected bool swigCMemOwn;
33
34         /// <summary>
35         /// A Flat to check if it is already disposed.
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         protected bool disposed = false;
39
40         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
41
42         //A Flag to check who called Dispose(). (By User or DisposeQueue)
43         private bool isDisposeQueued = false;
44
45         /// <summary>
46         /// Dispose.
47         /// </summary>
48         /// <since_tizen> 3 </since_tizen>
49         ~RelayoutContainer()
50         {
51             if (!isDisposeQueued)
52             {
53                 isDisposeQueued = true;
54                 DisposeQueue.Instance.Add(this);
55             }
56         }
57
58         /// <summary>
59         /// To make the RelayoutContainer instance be disposed.
60         /// </summary>
61         /// <since_tizen> 3 </since_tizen>
62         public void Dispose()
63         {
64             //Throw excpetion if Dispose() is called in separate thread.
65             if (!Window.IsInstalled())
66             {
67                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
68             }
69
70             if (isDisposeQueued)
71             {
72                 Dispose(DisposeTypes.Implicit);
73             }
74             else
75             {
76                 Dispose(DisposeTypes.Explicit);
77                 System.GC.SuppressFinalize(this);
78             }
79         }
80
81         /// <summary>
82         /// Adds relayout information to the container if it doesn't already exist.
83         /// </summary>
84         /// <param name="view">The view to relayout.</param>
85         /// <param name="size">The size to relayout.</param>
86         /// <since_tizen> 3 </since_tizen>
87         public virtual void Add(View view, Size2D size)
88         {
89             NDalicPINVOKE.RelayoutContainer_Add(swigCPtr, View.getCPtr(view), Size2D.getCPtr(size));
90             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
91         }
92
93         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(RelayoutContainer obj)
94         {
95             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
96         }
97
98         internal RelayoutContainer(global::System.IntPtr cPtr, bool cMemoryOwn)
99         {
100             swigCMemOwn = cMemoryOwn;
101             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
102         }
103
104         /// <summary>
105         /// Dispose.
106         /// </summary>
107         /// <since_tizen> 3 </since_tizen>
108         protected virtual void Dispose(DisposeTypes type)
109         {
110             if (disposed)
111             {
112                 return;
113             }
114
115             if(type == DisposeTypes.Explicit)
116             {
117                 //Called by User
118                 //Release your own managed resources here.
119                 //You should release all of your own disposable objects here.
120             }
121
122             //Release your own unmanaged resources here.
123             //You should not access any managed member here except static instance.
124             //because the execution order of Finalizes is non-deterministic.
125
126             if (swigCPtr.Handle != global::System.IntPtr.Zero)
127             {
128                 if (swigCMemOwn)
129                 {
130                     swigCMemOwn = false;
131                     NDalicPINVOKE.delete_RelayoutContainer(swigCPtr);
132                 }
133                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
134             }
135             disposed = true;
136         }
137     }
138 }