635cb86d79474f456a5a854d49d99012478bd1fb
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / VectorGraphics / CanvasView.cs
1 /*
2 * Copyright(c) 2021 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 using System;
18 using System.ComponentModel;
19 using System.Collections.Generic;
20 using System.Diagnostics.CodeAnalysis;
21 using Tizen.NUI.Binding;
22
23 namespace Tizen.NUI.BaseComponents.VectorGraphics
24 {
25     /// <summary>
26     /// CanvasView is a class for displaying vector primitives.
27     /// </summary>
28     /// <since_tizen> 9 </since_tizen>
29     public class CanvasView : View
30     {
31         private List<Drawable> drawables; //The list of added drawables
32
33         static CanvasView() { }
34
35         /// <summary>
36         /// ViewBoxProperty
37         /// </summary>
38         [EditorBrowsable(EditorBrowsableState.Never)]
39         public static readonly BindableProperty ViewBoxProperty = BindableProperty.Create(nameof(ViewBox), typeof(Tizen.NUI.Size2D), typeof(Tizen.NUI.BaseComponents.VectorGraphics.CanvasView), null, propertyChanged: (bindable, oldValue, newValue) =>
40         {
41             var instance = (Tizen.NUI.BaseComponents.VectorGraphics.CanvasView)bindable;
42             if (newValue != null)
43             {
44                 instance.InternalViewBox = (Tizen.NUI.Size2D)newValue;
45             }
46         },
47         defaultValueCreator: (bindable) =>
48         {
49             var instance = (Tizen.NUI.BaseComponents.VectorGraphics.CanvasView)bindable;
50             return instance.InternalViewBox;
51         });
52
53         /// <summary>
54         /// Creates an initialized CanvasView.
55         /// </summary>
56         /// <since_tizen> 9 </since_tizen>
57         public CanvasView() : this(Interop.CanvasView.New(), true)
58         {
59             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
60         }
61
62         /// <summary>
63         /// Creates an initialized CanvasView.
64         /// </summary>
65         /// <param name="viewBox">The size of viewbox.</param>
66         /// <exception cref="ArgumentNullException"> Thrown when viewBox is null. </exception>
67         /// <since_tizen> 9 </since_tizen>
68         [SuppressMessage("Microsoft.Design", "CA2000: Dispose objects before losing scope", Justification = "It does not have dispose ownership.")]
69         public CanvasView(Size2D viewBox) : this(viewBox == null ? throw new ArgumentNullException(nameof(viewBox)) : Interop.CanvasView.New(Uint16Pair.getCPtr(new Uint16Pair((uint)viewBox.Width, (uint)viewBox.Height))), true)
70         {
71             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
72         }
73
74         internal CanvasView(global::System.IntPtr cPtr, bool shown = true) : base(cPtr, shown)
75         {
76             drawables = new List<Drawable>();
77             if (!shown)
78             {
79                 SetVisible(false);
80             }
81         }
82
83         /// <summary>
84         /// You can override it to clean-up your own resources.
85         /// </summary>
86         /// <param name="type">DisposeTypes</param>
87         [EditorBrowsable(EditorBrowsableState.Never)]
88         protected override void Dispose(DisposeTypes type)
89         {
90             if (disposed)
91             {
92                 return;
93             }
94
95             if (type == DisposeTypes.Explicit)
96             {
97                 drawables.Clear();
98             }
99
100             base.Dispose(type);
101         }
102
103         /// This will not be public opened.
104         [EditorBrowsable(EditorBrowsableState.Never)]
105         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
106         {
107             Interop.CanvasView.DeleteCanvasView(swigCPtr);
108             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
109         }
110
111         /// <summary>
112         /// The viewbox of the CanvasView.
113         /// The viewbox means the size of CanvasView's internal space.
114         /// If the size of the viewbox is larger than the size of the CanvasView, the shapes are displayed smaller than the specified size.
115         /// The default value of the viewbox is the same as the size of the canvasview.
116         /// </summary>
117         /// <since_tizen> 9 </since_tizen>
118         public Size2D ViewBox
119         {
120             get
121             {
122                 return GetValue(ViewBoxProperty) as Size2D;
123             }
124             set
125             {
126                 SetValue(ViewBoxProperty, value);
127                 NotifyPropertyChanged();
128             }
129         }
130
131         private Size2D InternalViewBox
132         {
133             get
134             {
135                 Size2D retVal = new Size2D(0, 0);
136                 PropertyValue viewBoxPropertyValue = GetProperty(Interop.CanvasView.PropertyViewBoxGet());
137                 viewBoxPropertyValue?.Get(retVal);
138                 viewBoxPropertyValue?.Dispose();
139                 return retVal;
140             }
141             set
142             {
143                 PropertyValue setVal = new Tizen.NUI.PropertyValue(value);
144                 SetProperty(Interop.CanvasView.PropertyViewBoxGet(), setVal);
145                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
146                 setVal?.Dispose();
147             }
148         }
149
150         /// <summary>
151         /// Add drawable object to the CanvasView.
152         /// This method is similar to registration. The added shape is drawn on the inner canvas.
153         /// </summary>
154         /// <param name="drawable">Drawable object</param>
155         /// <exception cref="ArgumentNullException"> Thrown when drawable is null. </exception>
156         /// <since_tizen> 9 </since_tizen>
157         public void AddDrawable(Drawable drawable)
158         {
159             if (drawable == null)
160             {
161                 throw new ArgumentNullException(nameof(drawable));
162             }
163             Interop.CanvasView.AddDrawable(View.getCPtr(this), BaseHandle.getCPtr(drawable));
164             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
165             if (!drawables.Contains(drawable))
166             {
167                 drawables.Add(drawable);
168             }
169         }
170
171         /// <summary>
172         /// Remove drawable object to the CanvasView.
173         /// This method is similar to deregistration.
174         /// </summary>
175         /// <param name="drawable">Drawable object</param>
176         /// <exception cref="ArgumentNullException"> Thrown when drawable is null. </exception>
177         /// <since_tizen> 9 </since_tizen>
178         public void RemoveDrawable(Drawable drawable)
179         {
180             if (drawable == null)
181             {
182                 throw new ArgumentNullException(nameof(drawable));
183             }
184             Interop.CanvasView.RemoveDrawable(View.getCPtr(this), BaseHandle.getCPtr(drawable));
185             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
186             drawables.Remove(drawable);
187         }
188
189         /// <summary>
190         /// Remove all drawable objects added to the CanvasView.
191         /// </summary>
192         /// <since_tizen> 9 </since_tizen>
193         public void RemoveAllDrawables()
194         {
195             Interop.CanvasView.RemoveAllDrawables(View.getCPtr(this));
196             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
197             drawables.Clear();
198         }
199     }
200 }