[NUI] SafeNativeWindowHandle, VideoView, Layer changed
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ImageView.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 namespace Tizen.NUI.BaseComponents
19 {
20     using System;
21     using System.Runtime.InteropServices;
22     using System.ComponentModel;
23
24     /// <summary>
25     /// ImageView is a class for displaying an image resource.<br />
26     /// An instance of ImageView can be created using a URL or an image instance.<br />
27     /// </summary>
28     public class ImageView : View
29     {
30         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
31
32         internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
33         {
34             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
35         }
36
37         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
38         {
39             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
40         }
41
42
43         /// <summary>
44         /// Event arguments of resource ready.
45         /// </summary>
46         public class ResourceReadyEventArgs : EventArgs
47         {
48             private View _view;
49
50             /// <summary>
51             /// The view whose resource is ready.
52             /// </summary>
53             /// <since_tizen> 3 </since_tizen>
54             public View View
55             {
56                 get
57                 {
58                     return _view;
59                 }
60                 set
61                 {
62                     _view = value;
63                 }
64             }
65         }
66
67         private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
68         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
69         private delegate void ResourceReadyEventCallbackType(IntPtr data);
70         private ResourceReadyEventCallbackType _resourceReadyEventCallback;
71
72         /// <summary>
73         /// An event for ResourceReady signal which can be used to subscribe or unsubscribe the event handler.<br />
74         /// This signal is emitted after all resources required by a control are loaded and ready.<br />
75         /// Most resources are only loaded when the control is placed on the stage.<br />
76         /// </summary>
77         /// <since_tizen> 3 </since_tizen>
78         public event EventHandler<ResourceReadyEventArgs> ResourceReady
79         {
80             add
81             {
82                 if (_resourceReadyEventHandler == null)
83                 {
84                     _resourceReadyEventCallback = OnResourceReady;
85                     ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
86                 }
87
88                 _resourceReadyEventHandler += value;
89             }
90
91             remove
92             {
93                 _resourceReadyEventHandler -= value;
94
95                 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
96                 {
97                     ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
98                 }
99             }
100         }
101
102         // Callback for View ResourceReady signal
103         private void OnResourceReady(IntPtr data)
104         {
105             ResourceReadyEventArgs e = new ResourceReadyEventArgs();
106             if (data != null)
107             {
108                 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
109             }
110
111             if (_resourceReadyEventHandler != null)
112             {
113                 _resourceReadyEventHandler(this, e);
114             }
115         }
116
117         /// <summary>
118         /// you can override it to clean-up your own resources.
119         /// </summary>
120         /// <param name="type">DisposeTypes</param>
121         /// <since_tizen> 3 </since_tizen>
122         protected override void Dispose(DisposeTypes type)
123         {
124             if (disposed)
125             {
126                 return;
127             }
128
129             if (type == DisposeTypes.Explicit)
130             {
131                 //Called by User
132                 //Release your own managed resources here.
133                 //You should release all of your own disposable objects here.
134                 _border?.Dispose();
135                 _border = null;
136                 _nPatchMap?.Dispose();
137                 _nPatchMap = null;
138             }
139
140             //Release your own unmanaged resources here.
141             //You should not access any managed member here except static instance.
142             //because the execution order of Finalizes is non-deterministic.
143
144             if (swigCPtr.Handle != global::System.IntPtr.Zero)
145             {
146                 if (swigCMemOwn)
147                 {
148                     swigCMemOwn = false;
149                     NDalicPINVOKE.delete_ImageView(swigCPtr);
150                 }
151                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
152             }
153
154             base.Dispose(type);
155         }
156
157         internal new class Property
158         {
159             internal static readonly int RESOURCE_URL = NDalicPINVOKE.ImageView_Property_RESOURCE_URL_get();
160             internal static readonly int IMAGE = NDalicPINVOKE.ImageView_Property_IMAGE_get();
161             internal static readonly int PRE_MULTIPLIED_ALPHA = NDalicPINVOKE.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
162             internal static readonly int PIXEL_AREA = NDalicPINVOKE.ImageView_Property_PIXEL_AREA_get();
163         }
164
165         /// <summary>
166         /// Creates an initialized ImageView.
167         /// </summary>
168         /// <since_tizen> 3 </since_tizen>
169         public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
170         {
171             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
172
173         }
174         /// <summary>
175         /// Creates an initialized ImageView from a URL to an image resource.<br />
176         /// If the string is empty, ImageView will not display anything.<br />
177         /// </summary>
178         /// <param name="url">The URL of the image resource to display.</param>
179         /// <since_tizen> 3 </since_tizen>
180         public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
181         {
182             _url = url;
183             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
184
185         }
186         internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
187         {
188             _url = url;
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190
191         }
192
193         /// <summary>
194         /// [Obsolete("Please do not use! this will be deprecated")]
195         /// </summary>
196         [Obsolete("Please do not use! this will be deprecated")]
197         public new static ImageView DownCast(BaseHandle handle)
198         {
199             ImageView ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
200             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201             return ret;
202         }
203         /// <summary>
204         /// Sets this ImageView from the given URL.<br />
205         /// If the URL is empty, ImageView will not display anything.<br />
206         /// </summary>
207         /// <param name="url">The URL to the image resource to display.</param>
208         /// <since_tizen> 3 </since_tizen>
209         public void SetImage(string url)
210         {
211             _url = url;
212             NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
213             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214         }
215         internal void SetImage(string url, Uint16Pair size)
216         {
217             _url = url;
218             NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
219             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
220         }
221
222         internal ViewResourceReadySignal ResourceReadySignal(View view)
223         {
224             ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
225             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
226             return ret;
227         }
228
229         /// <summary>
230         /// Queries if all resources required by a control are loaded and ready.<br />
231         /// Most resources are only loaded when the control is placed on the stage.<br />
232         /// True if the resources are loaded and ready, false otherwise.<br />
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         public new bool IsResourceReady()
236         {
237             bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
238             if (NDalicPINVOKE.SWIGPendingException.Pending)
239                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
240             return ret;
241         }
242
243         /// <summary>
244         /// ImageView ResourceUrl, type string.
245         /// </summary>
246         /// <since_tizen> 3 </since_tizen>
247         public string ResourceUrl
248         {
249             get
250             {
251                 GetProperty(ImageView.Property.IMAGE).Get(out _url);
252                 return _url;
253             }
254             set
255             {
256                 _url = value;
257                 UpdateImage();
258             }
259         }
260
261         /// <summary>
262         /// This will be deprecated, please use Image instead. <br />
263         /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
264         /// </summary>
265         /// <since_tizen> 3 </since_tizen>
266         [EditorBrowsable(EditorBrowsableState.Never)]
267         public PropertyMap ImageMap
268         {
269             get
270             {
271                 if (_border == null)
272                 {
273                     PropertyMap temp = new PropertyMap();
274                     GetProperty(ImageView.Property.IMAGE).Get(temp);
275                     return temp;
276                 }
277                 else
278                 {
279                     return null;
280                 }
281             }
282             set
283             {
284                 if (_border == null)
285                 {
286                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
287                 }
288             }
289         }
290
291         /// <summary>
292         /// ImageView Image, type PropertyMap
293         /// </summary>
294         /// <since_tizen> 4 </since_tizen>
295         public PropertyMap Image
296         {
297             get
298             {
299                 if (_border == null)
300                 {
301                     PropertyMap temp = new PropertyMap();
302                     GetProperty(ImageView.Property.IMAGE).Get(temp);
303                     return temp;
304                 }
305                 else
306                 {
307                     return null;
308                 }
309             }
310             set
311             {
312                 if (_border == null)
313                 {
314                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
315                 }
316             }
317         }
318
319         /// <summary>
320         /// ImageView PreMultipliedAlpha, type Boolean.<br />
321         /// Image must be initialized.<br />
322         /// </summary>
323         /// <since_tizen> 3 </since_tizen>
324         public bool PreMultipliedAlpha
325         {
326             get
327             {
328                 bool temp = false;
329                 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
330                 return temp;
331             }
332             set
333             {
334                 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
335             }
336         }
337
338         /// <summary>
339         /// ImageView PixelArea, type Vector4 (Animatable property).<br />
340         /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
341         /// </summary>
342         /// <since_tizen> 3 </since_tizen>
343         public RelativeVector4 PixelArea
344         {
345             get
346             {
347                 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
348                 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
349                 return temp;
350             }
351             set
352             {
353                 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
354             }
355         }
356
357         /// <summary>
358         /// The border of the image in the order: left, right, bottom, top.<br />
359         /// If set, ImageMap will be ignored.<br />
360         /// For N-Patch images only.<br />
361         /// Optional.
362         /// </summary>
363         /// <since_tizen> 3 </since_tizen>
364         public Rectangle Border
365         {
366             get
367             {
368                 return _border;
369             }
370             set
371             {
372                 _border = value;
373                 UpdateImage();
374             }
375         }
376
377         /// <summary>
378         /// Gets or sets whether to draw the borders only (if true).<br />
379         /// If not specified, the default is false.<br />
380         /// For N-Patch images only.<br />
381         /// Optional.
382         /// </summary>
383         /// <since_tizen> 3 </since_tizen>
384         public bool BorderOnly
385         {
386             get
387             {
388                 return _borderOnly ?? false;
389             }
390             set
391             {
392                 _borderOnly = value;
393                 UpdateImage();
394             }
395         }
396
397         /// <summary>
398         /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
399         /// </summary>
400         /// <since_tizen> 3 </since_tizen>
401         public bool SynchronosLoading
402         {
403             get
404             {
405                 return _synchronousLoading ?? false;
406             }
407             set
408             {
409                 _synchronousLoading = value;
410                 UpdateImage();
411             }
412         }
413
414         private void UpdateImage()
415         {
416             if (_url != null)
417             {
418                 if (_border != null)
419                 { // for nine-patch image
420                     _nPatchMap = new PropertyMap();
421                     _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
422                     _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
423                     _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
424                     if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
425                     if (_synchronousLoading != null) _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
426                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
427                 }
428                 else if (_synchronousLoading != null)
429                 { // for normal image, with synchronous loading property
430                     PropertyMap imageMap = new PropertyMap();
431                     imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
432                     imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
433                     imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
434                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
435                 }
436                 else
437                 { // just for normal image
438                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
439                 }
440             }
441         }
442
443         private Rectangle _border = null;
444         private PropertyMap _nPatchMap = null;
445         private bool? _synchronousLoading = null;
446         private bool? _borderOnly = null;
447         private string _url = null;
448
449     }
450
451 }