Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Common / VisualObjectsContainer.cs
1 // Copyright (c) 2024 Samsung Electronics Co., Ltd.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15
16 using System;
17 using System.Text;
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.ComponentModel;
22
23 namespace Tizen.NUI.Visuals
24 {
25     /// <summary>
26     /// VisualObjectsContainer is a container for visual objects.
27     /// For each VisualObjectContainer, there is a corresponding view.
28     /// Each view can have only one VisualObjectsContainer per rangeType.
29     /// </summary>
30     /// <remarks>
31     /// To avoid the collision between Dali toolkit logic and NUI specific policy,
32     /// this container has an internal limitation of the number of visual objects.
33     /// If user try to add visual object over the limitation, it will be ignored.
34     /// </remarks>
35     internal class VisualObjectsContainer : BaseHandle
36     {
37         private List<Tizen.NUI.Visuals.VisualBase> visuals = new List<Tizen.NUI.Visuals.VisualBase>(); // Keep visual object reference.
38
39         /// <summary>
40         /// Creates an empty visual object handle.
41         /// </summary>
42         public VisualObjectsContainer() : this(Interop.VisualObjectsContainer.NewVisualObjectsContainer(), true, false)
43         {
44             NDalicPINVOKE.ThrowExceptionIfExists();
45         }
46
47         /// <summary>
48         /// Creates an visual object with VisualObjectsContainer.
49         /// </summary>
50         public VisualObjectsContainer(Tizen.NUI.BaseComponents.View view, int rangeType) : this(Interop.VisualObjectsContainer.VisualObjectsContainerNew(Tizen.NUI.BaseComponents.View.getCPtr(view), rangeType), true)
51         {
52             NDalicPINVOKE.ThrowExceptionIfExists();
53         }
54
55         internal VisualObjectsContainer(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, cMemoryOwn)
56         {
57         }
58
59         internal VisualObjectsContainer(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
60         {
61         }
62
63         public Tizen.NUI.BaseComponents.View GetView()
64         {
65             global::System.IntPtr cPtr = Interop.VisualObjectsContainer.GetOwner(SwigCPtr);
66             
67             Tizen.NUI.BaseComponents.View ret = null;
68             if (Interop.RefObject.GetRefObjectPtr(cPtr) == global::System.IntPtr.Zero)
69             {
70                 // Visual container don't have owner. Return null.
71                 Interop.BaseHandle.DeleteBaseHandle(new global::System.Runtime.InteropServices.HandleRef(this, cPtr));
72             }
73             else
74             {
75                 ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Tizen.NUI.BaseComponents.View;
76                 if (ret != null)
77                 {
78                     Interop.BaseHandle.DeleteBaseHandle(new global::System.Runtime.InteropServices.HandleRef(this, cPtr));
79                 }
80                 else
81                 {
82                     ret = new Tizen.NUI.BaseComponents.View(cPtr, true);
83                 }
84             }
85             NDalicPINVOKE.ThrowExceptionIfExists();
86             return ret;
87         }
88
89         public int GetContainerRangeType()
90         {
91             return Interop.VisualObjectsContainer.GetContainerRangeType(SwigCPtr);
92         }
93
94         public Tizen.NUI.Visuals.VisualBase this[uint index]
95         {
96             get
97             {
98                 return GetVisualObjectAt(index);
99             }
100         }
101
102         public uint GetVisualObjectsCount()
103         {
104             uint ret = Interop.VisualObjectsContainer.GetVisualObjectsCount(SwigCPtr);
105             NDalicPINVOKE.ThrowExceptionIfExists();
106             return ret;
107         }
108
109         public bool AddVisualObject(Tizen.NUI.Visuals.VisualBase visualObject)
110         {
111             // Detach from previous container first.
112             var previousContainer = visualObject.GetVisualContainer();
113             if (previousContainer != null)
114             {
115                 if (previousContainer == this)
116                 {
117                     // Already added to this container.
118                     return false;
119                 }
120                 visualObject.Detach();
121             }
122
123             visuals.Add(visualObject);
124
125             bool ret = Interop.VisualObjectsContainer.AddVisualObject(SwigCPtr, Tizen.NUI.Visuals.VisualBase.getCPtr(visualObject));
126             NDalicPINVOKE.ThrowExceptionIfExists();
127             return ret;
128         }
129
130         public void RemoveVisualObject(Tizen.NUI.Visuals.VisualBase visualObject)
131         {
132             visuals.Remove(visualObject);
133
134             Interop.VisualObjectsContainer.RemoveVisualObject(SwigCPtr, Tizen.NUI.Visuals.VisualBase.getCPtr(visualObject));
135             NDalicPINVOKE.ThrowExceptionIfExists();
136         }
137
138         public Tizen.NUI.Visuals.VisualBase FindVisualObjectByName(string name)
139         {
140             Tizen.NUI.Visuals.VisualBase ret = null;
141             if(!string.IsNullOrEmpty(name))
142             {
143                 foreach (var visual in visuals)
144                 {
145                     if (visual?.Name == name)
146                     {
147                         return visual;
148                     }
149                 }
150             }
151             return ret;
152         }
153
154         private Tizen.NUI.Visuals.VisualBase GetVisualObjectAt(uint index)
155         {
156             global::System.IntPtr cPtr = Interop.VisualObjectsContainer.GetVisualObjectAt(SwigCPtr, index);
157             Visuals.VisualBase ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Visuals.VisualBase;
158             if (ret != null)
159             {
160                 Interop.BaseHandle.DeleteBaseHandle(new global::System.Runtime.InteropServices.HandleRef(this, cPtr));
161             }
162             else
163             {
164                 ret = new Visuals.VisualBase(cPtr, true);
165             }
166             NDalicPINVOKE.ThrowExceptionIfExists();
167             return ret;
168         }
169
170         /// <summary>
171         /// Dispose for VisualObjectsContainer
172         /// </summary>
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         protected override void Dispose(DisposeTypes type)
175         {
176             if (disposed)
177             {
178                 return;
179             }
180
181             if (type == DisposeTypes.Explicit)
182             {
183                 //Called by User
184                 //Release your own managed resources here.
185                 //You should release all of your own disposable objects here.
186             }
187
188             base.Dispose(type);
189         }
190     }
191 }