1 // Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
21 using System.ComponentModel;
23 namespace Tizen.NUI.Visuals
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.
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.
35 internal class VisualObjectsContainer : BaseHandle
37 private List<Tizen.NUI.Visuals.VisualBase> visuals = new List<Tizen.NUI.Visuals.VisualBase>(); // Keep visual object reference.
40 /// Creates an empty visual object handle.
42 public VisualObjectsContainer() : this(Interop.VisualObjectsContainer.NewVisualObjectsContainer(), true, false)
44 NDalicPINVOKE.ThrowExceptionIfExists();
48 /// Creates an visual object with VisualObjectsContainer.
50 public VisualObjectsContainer(Tizen.NUI.BaseComponents.View view, int rangeType) : this(Interop.VisualObjectsContainer.VisualObjectsContainerNew(Tizen.NUI.BaseComponents.View.getCPtr(view), rangeType), true)
52 NDalicPINVOKE.ThrowExceptionIfExists();
55 internal VisualObjectsContainer(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, cMemoryOwn)
59 internal VisualObjectsContainer(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
63 public Tizen.NUI.BaseComponents.View GetView()
65 global::System.IntPtr cPtr = Interop.VisualObjectsContainer.GetOwner(SwigCPtr);
67 Tizen.NUI.BaseComponents.View ret = null;
68 if (Interop.RefObject.GetRefObjectPtr(cPtr) == global::System.IntPtr.Zero)
70 // Visual container don't have owner. Return null.
71 Interop.BaseHandle.DeleteBaseHandle(new global::System.Runtime.InteropServices.HandleRef(this, cPtr));
75 ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Tizen.NUI.BaseComponents.View;
78 Interop.BaseHandle.DeleteBaseHandle(new global::System.Runtime.InteropServices.HandleRef(this, cPtr));
82 ret = new Tizen.NUI.BaseComponents.View(cPtr, true);
85 NDalicPINVOKE.ThrowExceptionIfExists();
89 public int GetContainerRangeType()
91 return Interop.VisualObjectsContainer.GetContainerRangeType(SwigCPtr);
94 public Tizen.NUI.Visuals.VisualBase this[uint index]
98 return GetVisualObjectAt(index);
102 public uint GetVisualObjectsCount()
104 uint ret = Interop.VisualObjectsContainer.GetVisualObjectsCount(SwigCPtr);
105 NDalicPINVOKE.ThrowExceptionIfExists();
109 public bool AddVisualObject(Tizen.NUI.Visuals.VisualBase visualObject)
111 // Detach from previous container first.
112 var previousContainer = visualObject.GetVisualContainer();
113 if (previousContainer != null)
115 if (previousContainer == this)
117 // Already added to this container.
120 visualObject.Detach();
123 visuals.Add(visualObject);
125 bool ret = Interop.VisualObjectsContainer.AddVisualObject(SwigCPtr, Tizen.NUI.Visuals.VisualBase.getCPtr(visualObject));
126 NDalicPINVOKE.ThrowExceptionIfExists();
130 public void RemoveVisualObject(Tizen.NUI.Visuals.VisualBase visualObject)
132 visuals.Remove(visualObject);
134 Interop.VisualObjectsContainer.RemoveVisualObject(SwigCPtr, Tizen.NUI.Visuals.VisualBase.getCPtr(visualObject));
135 NDalicPINVOKE.ThrowExceptionIfExists();
138 public Tizen.NUI.Visuals.VisualBase FindVisualObjectByName(string name)
140 Tizen.NUI.Visuals.VisualBase ret = null;
141 if(!string.IsNullOrEmpty(name))
143 foreach (var visual in visuals)
145 if (visual?.Name == name)
154 private Tizen.NUI.Visuals.VisualBase GetVisualObjectAt(uint index)
156 global::System.IntPtr cPtr = Interop.VisualObjectsContainer.GetVisualObjectAt(SwigCPtr, index);
157 Visuals.VisualBase ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Visuals.VisualBase;
160 Interop.BaseHandle.DeleteBaseHandle(new global::System.Runtime.InteropServices.HandleRef(this, cPtr));
164 ret = new Visuals.VisualBase(cPtr, true);
166 NDalicPINVOKE.ThrowExceptionIfExists();
171 /// Dispose for VisualObjectsContainer
173 [EditorBrowsable(EditorBrowsableState.Never)]
174 protected override void Dispose(DisposeTypes type)
181 if (type == DisposeTypes.Explicit)
184 //Release your own managed resources here.
185 //You should release all of your own disposable objects here.